我在codeblocks上测试了这段代码并且工作正常但是当我在Ideone上运行时,我在OJ上遇到运行时错误和SIGSEGV错误.我在网上看到SIGSEGV错误是由于内存访问受限造成的.但如果它这样做,为什么不是代码块抱怨......
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int t, i, j, k, x, temp;
int num1[10000], num2[10000];
char c;
bool f = true;
cin >> t;
while (t--)
{
for (i = 0; i < 10000; i++)
{
num1[i] = 0;
num2[i] = 0;
}
i = 0;
j = 0;
while (!isspace(c = getchar()))
num1[i++] = c - '0';
while (!isspace(c = getchar()))
num2[j++] = c - '0';
x = i > j ? i : j;
temp = 0;
for (k = 0; k < x; k++)
{
i = temp + num1[k] + num2[k];
if (f)
{
if (i % 10 != 0)
{
f = false;
cout << i % 10;
}
}
else cout << i % 10;
temp = i / 10;
}
if (temp != 0)
cout << temp;
cout << endl;
f = true;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个:
while (!isspace(c = getchar()))
Run Code Online (Sandbox Code Playgroud)
将继续前进,直到找到空格字符.如果在最终输入值之后没有空格,那么getchar()将返回EOF,您将截断char并传递给它isspace,给出未定义的行为.很可能isspace会返回false,并且你将永远循环,离开固定大小数组的末尾,直到你达到无法寻址的内存并导致分段错误.
使用前请务必检查输入.如果没有输入,你也会遇到大问题,因为你没有检查是否t成功读取.