两个细节要注意:
(1)、保存最终结果的数组要开102的大小!
(2)、存在001这样的数据!所以开始的判断要:if (!strcmp(s,"0"))
code:
<span style="font-size:18px;">#include <iostream>
#include <fstream>
using namespace std;
char s[101];
int ans[102];
int main()
{
fstream in("input.txt");
while (cin >> s)
{
if (!strcmp(s,"0"))
break;
int len = strlen(s);
for (int i = 0; i<len; i++){
ans[i] += s[len - i - 1] - '0';
}
for (int i = 0; i <= 101; i++){
if (ans[i] >= 10){
ans[i + 1] += ans[i] / 10;
ans[i] %= 10;
}
}
}
int end = 101;
while (ans[end] == 0)
end--;
for (int j = end; j >= 0; j--)
cout << ans[j];
cout << endl;
//system("pause");
return 0;
}
</span>