I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
题目大概意思:输入两个很大很大的数字,意味着这不是简单的整型类型,然后按要求格式输出两个数字的和
我的见解:看起来很简单,很多首先考虑字符串,然后将短的补充至与长的相等,jian用ASCⅡ码进行位与位的运算,再建多一个字符数组装他们的和,要着重考虑递进的问题哦,,,,,想到了一个更好的方法,可以减少字符组的使用,以后有时间再贴出来了,再下面有个更高级的算法,用了高级补充字符函数memset。
代码如下:
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
int n, j, k, p, h, i = 0;
cin >> n;
while (n)
{
i++; n--;
char a[1001], b[1001], c[1002] = { 0 }, d[1001] = { 0 }, l[1001];
cin >> a >> b;
j = strlen(a);
k = strlen(b);
if (j > k)
{
for (int r = j - k; r < j; r++)
{
d[r] = b[r - (j - k)];
}
strcpy_s(l, a);
h = j;
}
else if (j < k)
{
for (int r = k - j; r < k; r++)
{
d[r] = a[r - (k - j)];
}
strcpy_s(l, b);
h = k;
}
else
{
h = j;
strcpy_s(l, b);
strcpy_s(d, a);
}
for (p = h; p >= 1; p--)
{
if (((l[p - 1] + d[p - 1]) + c[p] < 106) && d[p - 1] != \'\\0\')
{
c[p] += l[p - 1] + d[p - 1] - 48;
}
else if ((l[p - 1] + c[p] < 58) && d[p - 1] == \'\\0\')
{
c[p] += l[p - 1];
}
else if ((l[p - 1] + c[p] >= 58) && d[p - 1] == \'\\0\')
{
c[p] += l[p - 1] - 10;
c[p - 1] += 1;
}
else
{
c[p] += l[p - 1] + d[p - 1] - 58;
c[p - 1] += 1;
}
}
if (c[0] == \'\\0\')
{
c[0] = \' \';
cout << \"Case \" << i << \":\" << endl;
cout << a << \" + \" << b << \" =\" << c << endl;
}
else
{
c[0] += 48;
cout << \"Case \" << i << \":\" << endl;
cout << a << \" + \" << b << \" = \" << c << endl;
}
if (n) cout << endl;
}
}
高级代码方法:(少部分借鉴网上大神代码)
#include <iostream>
using namespace std;
void main()
{
char a[1002], b[1002], c[1002], d[1002], e[1002], f[1002];
int n, Lenmax, t = 0, p = 1;
cin >> n;
while (n--)
{
memset(a, \'0\', sizeof(a));
memset(b, \'0\', sizeof(b));
memset(c, \'0\', sizeof(c));
memset(d, \'0\', sizeof(d));
memset(e, \'0\', sizeof(e));
memset(f, \'\\0\', sizeof(f));
cin >> a >> b;
if (strlen(a) > strlen(b))Lenmax = strlen(a);
else Lenmax = strlen(b);
for (int i = strlen(a) - 1, x = 0; i >= 0; i--, x++)//将数字反序 99789→98799
{
c[x] = a[i];
}
for (int i = strlen(b) - 1, x = 0; i >= 0; i--, x++)//将数字反序 789→987
{
d[x] = b[i];
}
for (int k = 0; k < Lenmax; k++)//将数字相加
{
t += c[k] - \'0\';
t += d[k] - \'0\';
e[k] = (t % 10) + \'0\';//9+9=18 取8 9+8+1=18 取8 7+7+1=15 取5 9+0+1=10 取0 9+0+1=10 取0 0+0+1 取1
if (t >= 10 && k == Lenmax - 1)Lenmax++;
if (t >= 10)t = 1; //18>10进1位 18>10进一位 15>10 进一位 10=10 进一位 10=10 进一位
else t = 0;
}
for (int z = Lenmax - 1, i = 0; z >= 0; z--, i++)
{
f[i] = e[z];
}
cout << \"Case \" << p++ << \":\" << endl << a << \" + \" << b << \" = \" << f << endl;
if (n)cout << endl;
}
}
继续阅读与本文标签相同的文章
最实用的Linux命令总结
-
活动回顾丨阿里云业务中台最佳实践沙龙圆满落幕
2026-05-19栏目: 教程
-
在CentOS里查看ssh的登录记录
2026-05-19栏目: 教程
-
好程序员web前端学习路线分享纯css绘制各种图形
2026-05-19栏目: 教程
-
菜鸟裹裹快递员收入有多高?小哥晒出月流水,程序员表示羡慕了
2026-05-19栏目: 教程
-
开发者必读 · 周报 | 002期
2026-05-19栏目: 教程
