【发布时间】:2015-08-31 18:05:22
【问题描述】:
以下是SPOJ GCD2 的代码。它在我的机器和 Ideone 上运行良好,但在 SPOJ 上出现运行时错误 (SIGFPE)。我已经检查了 spojtoolkit.com 上的所有测试用例。
我无法弄清楚为什么此代码在 spoj 上显示运行时错误 (SIGFPE)。 SIGFPE 表示错误的算术运算。
为什么这段代码在 SPOJ 上显示运行时错误?
#include <bits/stdc++.h>
using namespace std;
int gcd(int x,int a)
{
if(a==0)
return x;
else
return gcd(a, x%a);
}
int getmod(string b,int a)
{
int n=b.size();
int d;
d= (b[0]-'0') % a;
for(int i=1; i!=n; i++)
{
d=d*10;
d=d + (b[i]-'0');
d= d % a;
}
return d;
}
int main()
{
int tc;
cin >> tc;
int a;
string b;
while(tc--)
{
cin >>a>>b;
int x=getmod(b,a);
cout << gcd(x,a)<<endl;
}
return 0;
}
【问题讨论】:
-
请求 SPOJ 会不断在 StackOverflow 上生成不良内容。如果您的问题中没有任何进一步的研究和调试工作,请停止询问此类问题。
标签: c++ string algorithm runtime-error