作者sam9595 (帕帕)
看板C_and_CPP
標題[問題] runtime error
時間Thu Feb 5 11:42:20 2009
想請問一下 為什麼有些C code 在acm用ansi C編譯的時候會runtime error
但是用C++ 編譯卻會過
是有哪些東西在C是不能用的嗎 不過我用gcc編譯都是可以的
所以我想不太清楚問題在哪裡
比如說下面這個例子
#10589
#include<stdio.h>
#include<math.h>
int inorout(double x ,double y,double a){
double t1,t2,t3,t4;
if(x>a || y>a)
return 0;
t1=sqrt( pow(x,2)+pow(y,2) );
t2=sqrt( pow(a-x,2)+pow(y,2));
t3=sqrt( pow(x,2)+pow(a-y,2));
t4=sqrt( pow(a-x,2)+pow(a-y,2));
if(t1>a || t2>a || t3>a ||t4>a)
return 0;
else
return 1;
}
int main(){
int M,N,i;
double x,y,a;
while(1){
scanf("%d %lf",&N,&a);
if(N==0 && a==0)
break;
M=0;
for(i=0;i<N;i++){
scanf("%lf %lf",&x,&y);
if(inorout(x,y,a))
M++;
}
printf("%.5lf\n",(double)M*a*a/(double)N);
}
}
或是
#10783
#include<stdio.h>
int main(){
int Ncase,temp;
int a,b,sum=0,round=1;
char buf[100],*p;
fgets(buf,100,stdin);
for(Ncase=0,p=buf;*p>='0';Ncase=Ncase*10+*p++-'0'){}
while(Ncase){
fgets(buf,100,stdin);
for(a=0,p=buf;*p>='0';a=a*10+*p++-'0'){}
fgets(buf,100,stdin);
for(b=0,p=buf;*p>='0';b=b*10+*p++-'0'){}
if(a>b){
temp=a;
a=b;
b=temp;
}
if(a%2==0)
a++;
if(b%2==0)
b--;
for(;a<=b;a+=2){
sum+=a;
}
printf("Case %d: %d\n",round++,sum);
sum=0;
Ncase--;
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.30.84
推 ledia:runtime error 是程式執行時的錯誤 02/05 11:52
→ ledia:跟 compile error 是不一樣的 02/05 11:52
→ ledia:合語法的 compile 都會過 02/05 11:52
→ sam9595:恩 我知道 不過為何同樣的code執行結果會不一樣呢 02/05 12:13
推 ledia:因為 input 不一樣呀 ._. 02/05 14:11
→ fjm31714:runtime error跟input有關嗎? 02/05 15:06
推 ledia:of course 02/05 15:17
推 ledia:雞同鴨講... 我弄錯原 post 要問的意思了 02/05 15:20
→ ledia:是說為什麼你的 main 都沒 return value 呀? 02/05 15:20
→ fjm31714:對吼有可能....不過ACM的測資不是都一樣嗎? 02/05 15:36
推 ledia:我以為他的意思是自己測沒問題, 送去測 runtime error 02/05 15:44
→ ledia:那就的確可能是 input 造成的差異 02/05 15:44
推 fjm31714:應該是C++ AC 可是 C RE 02/05 15:46
→ sam9595:恩 對 感謝 應該是因為我沒有return值的關係 02/05 15:59