奇怪的現象?-同份code, C++能過,C不能過
我猜是型態傳換的問題,但看不出詳細
有人可以告訴我為什麼嗎?
#include <stdio.h> #include <stdlib.h> #include <math.h> unsigned long long int Reduce(int X ,int Y) { unsigned long long int Count=0,Exp,Len; Len=X>Y?X:Y; Exp=1<<(unsigned long long int)ceil(log2(Len)); if (X==1&&Y==1) { return 1; } else if (X>Exp/2&&Y>Exp/2) { return (Exp*Exp/4)+Reduce(X-Exp/2,Y-Exp/2); } else if (X>Exp/2&&Y<=Exp/2) { return 2*(Exp*Exp/4)+Reduce(X-Exp/2,Y); } else if (X<=Exp/2&&Y>Exp/2) { return 3*(Exp*Exp/4)+Reduce(X,Y-Exp/2); } } int main() { int I,J; scanf("%d%d",&I,&J); printf("%llu\n",Reduce(J,I)); return 0; } |
全部換成 unsigned long long int 還是一樣C過不了
#include <stdio.h> #include <stdlib.h> #include <math.h> unsigned long long int Reduce(unsigned long long int X ,unsigned long long int Y) { unsigned long long int Exp,Len; Len=X>Y?X:Y; Exp=(unsigned long long int)pow(2,ceill(log2(Len))); if (X==1&&Y==1) { return 1; } else if (X>Exp/2&&Y>Exp/2) { return (Exp*Exp/4)+Reduce(X-Exp/2,Y-Exp/2); } else if (X>Exp/2&&Y<=Exp/2) { return 2*(Exp*Exp/4)+Reduce(X-Exp/2,Y); } else if (X<=Exp/2&&Y>Exp/2) { return 3*(Exp*Exp/4)+Reduce(X,Y-Exp/2); } } int main() { unsigned long long int I,J; scanf("%llu%llu",&I,&J); printf("%llu\n",Reduce(J,I)); return 0; } |