我没有除以零,我的代码中没有浮点数据类型,我仍然得到浮点异常。
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
unsigned long long int t,n;
cin>>t;
while(t--)
{
cin>>n;
unsigned long long int deno = pow(10,n-1),count=2,sum = 0,f1=1,f2=1;
while(1){
sum = f1+f2;
f1 = f2;
f2 = sum;
count++;
if((int)(sum/deno)>0){
cout<<count<<endl;
break;
}
}
}
return 0;
}
前面的所有问题都有类似的除以零的问题,但变量 deno 永远不能为零,因为n>=2
。
我之前的研究:
问题陈述:https ://www.hackerrank.com/contests/projecteuler/challenges/euler025/problem
它通过了 2 个测试用例并失败了 2 个。都是隐藏的测试用例。结果图片
通过输入 1 50 我们可以重现错误。细节:
GDB trace: Reading symbols from solution...done. [New LWP 15127] Core
was generated by `solution'. Program terminated with signal SIGFPE,
Arithmetic exception.
#0 main () at solution.cc:23
23 if((int)(sum/deno)>0){
#0 main () at solution.cc:23