坑
Dev C++ pow()函数
那年冬天,显示屏前坐着如喽啰,那时候我含泪发誓,再也不用Dev。
一个小题目,在Dev上就是跑不出正确答案,不信邪,就在vs上跑,完全正确。
#include <iostream> #include <math.h> using namespace std; int main() { int x; int sign = true; cin >> x; for (int i = 0; i <= sqrt(x) && sign; i++) { int I = pow(i, 2); for (int j = i; j <= sqrt(x) && sign; j++) { int J = pow(j, 2); for (int k = j; k <= sqrt(x) && sign; k++) { int K = pow(k, 2); double L =sqrt( x - I - J - K); if (L==int(L)) { cout << i <<" "<< j <<" "<< k<<" " << L<<endl; sign = false; } } } } return 0; }
经过我的穷举调试大法,
#include <iostream> #include <math.h> using namespace std; int main(){ for(int i=0;i<100;i++){ int L=pow(i,2); if(L!=i*i){ cout<<i*i<<" "<<L<<endl; } } return 0; }
运行结果:
我可以理解,因为pow形参,结果都为浮点数,形参转换的时候,24.99999999成了24,但是别的编译器里从来没碰到过这种情况,真的坑啊,根本想不到。