http://acm.nyist.net/JudgeOnline/problem.php?pid=103
大数相加吧。
#include<iostream>
#include<cstring>
using namespace std;
int main() {
int m, i;
cin >> m;
for (i = 1; i <= m; i++) {
int a[2000] = {0};
int b[2000] = {0};
int c[2000];
string ch, ck;
int s = 0, x = 0;
cin >> ch >> ck;
int max = ch.size() > ck.size() ? ch.size() : ck.size(); 取数位较长的,
int k = 0;
for (int j = ch.size() - 1; j >= 0; j--)
a[k++] = ch[j] - '0'; //字符转换成数字
k = 0;
for (int j = ck.size() - 1; j >= 0; j--)
b[k++] = ck[j] - '0';
k = 0;
for (int j = 0; j < max; j++) {
c[k] = a[j] + b[j] + x; //x表示进位数
if (j == max - 1) break;// 最后的一位数 不需要进位 运算后 直接跳出循环
else if (c[k] >= 10) {
x = 1;
c[k] %= 10;
}
else x = 0;
k++;
}
cout << "Case " << i << ":" << endl;
for (int j = ch.size() - 1; j >= 0; j--)
cout << a[j];
cout << " + ";
for (int j = ck.size() - 1; j >= 0; j--)
cout << b[j];
cout << " = ";
for (int j = k; j >= 0; j--)
cout << c[j];
cout << endl;
}
}