#include<iostream>
#include<string>
using namespace std;
/* 一定不要这么写,因为没有把值传回去
void max_L(string a, string b)
{
string c;
if (a.length() < b.length())
{
c = a;
a = b;
b = c;
}
}
*/
int main()
{
int test, a_L, b_L,sum[10000];//测试次数,a,b的位数减一,和
int num = 0;//进位
string a, b;//a,b
int num1, num2;//化成整型的每一位数
cin >> test;
if (test >= 1 && test <= 20)
{
for (int i = 0; i < test; i++)
{
cin >> a >> b;
cout << "Case " << i + 1 << ":" << endl;//C大写
cout << a << " + " << b << " = " ;
//max_L(a, b);
if (a.length() < b.length())
{
string c;
c = a;
a = b;
b = c;
}
a_L = a.length()-1;
b_L = b.length()-1;
int sum_L;
for (sum_L = 0; sum_L <= b_L; sum_L++)
{
num1 = a[a_L-sum_L] - '0';//一定要转成数字,不然是字符,计算的第一个数应该是数组的最后一个
num2 = b[b_L-sum_L] - '0';//一定要转成数字,不然是字符
sum[sum_L] = (num1 + num2 + num) % 10;//sum是整型数组
num = (num1 + num2 + num) / 10;
}
for (sum_L = (b_L+1); sum_L <= a_L; sum_L++)
{
num1 = a[a_L-sum_L] - '0';//一定要转成数字,不然是字符
sum[sum_L] = (num1 + num) % 10;
num = (num1 + num) / 10;
}
sum_L--;//循环完成多加了一个1,一定要减掉
if (num != 0)
{
sum_L++;//别忘了会多一位
sum[sum_L] = num;
}
num = 0;//本次循环完成一定要清零,否则下次循环还会用
for (int j = 0; j <= sum_L; j++)
{
cout << sum[sum_L - j];
}
cout << endl;
if (i < (test - 1))
{
cout << endl; //最后一行不空行
}
}
}
return 0;
}
1002 A + B Problem II
最新推荐文章于 2019-02-28 21:30:03 发布