#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
int main(){
string s;
vector<string>vec;
while (cin>>s&&s!="0"){
vec.push_back(s);
getchar();
}
vector<string>::size_type len;
len = vec.size();
string::size_type j,len2;
string cnt;
len2 = vec[0].length();
int sum = 0;
stack<int>stack;
for (j=len2-1; j>=0;j--){
for (int i = 0; i < len; i++){
sum += (vec[i][j]-'0');
}
int tmp = sum % 10;
stack.push(tmp);
sum /= 10;
if (j == 0)break;
}
if (sum != 0)stack.push(sum);
while (!stack.empty()){
int x = stack.top();
stack.pop();
cout << x;
}
cout << endl;
system("pause");
return 0;
}
之前遇到一个问题,string sizetype 类型值j是无符号值,所以当j=0时,再继续循环,j不会变为-1,而是不可预知,所以会报错,故当j=0时判断一下跳出循环。或者有一种不规范的写法是将j的类型规定为int型,虽然书上不建议这么做。。。。