HDU 1002
Problem De ion
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
问题描述:
高精度加法计算,数据范围超出long long范围。
问题分析:
用字符数组来录入字符,对每个字符减去‘0’字符再储存在整形数组中。再对整形数组的每一个进行加和,注意满十进一。
程序说明:
注意输出格式,换行。
AC代码如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char a[1005], b[1005];
int c[1005], d[1005], e[1005];
int t,n=1,work=0; //n用来case的例子,work是换行的条件
cin >> t;
while (t--)
{
cin >> a >> b;
if (work)
cout << endl;
cout << \"Case \" << n << \":\" << endl;
n++;
cout << a << \" + \" << b << \" = \";
int lena, lenb,ca,cb;
lena = strlen(a); //求a,b的位数
lenb = strlen(b);
ca = lena;
cb = lenb;
memset(c, 0, sizeof(c)); //将整形数组c,d,e的每一位都置为0
memset(d, 0, sizeof(d));
memset(e, 0, sizeof(e));
for (int i = 0; i < ca; i++) //按个位,十位,百位储存在整形数组a[0],a[1],a[2]中。
{
c[i] = a[lena - 1]-\'0\';
lena--;
}
for (int j = 0; j < cb; j++)
{
d[j] = b[lenb - 1]-\'0\';
lenb--;
}
int max = (ca > cb ? ca : cb); //这个必须要有
for (int k = 0; k < max; k++)
{
if ((e[k] + c[k] + d[k]) > 9) //满十进一
{
e[k] = c[k] + d[k] + e[k] - 10;
e[k + 1]++;
}
else
e[k] = e[k] + d[k] + c[k];
}
for (int m = max - 1; m >= 0; m--) //输出
{
cout << e[m];
}
cout << endl;
work = 1;
}
return 0;
}
继续阅读与本文标签相同的文章
怎么使用 Origin 把多张图合成一张图?
-
最新115道华为、京东、滴滴、美团精选Java面试题整理
2026-05-18栏目: 教程
-
吴伯凡:谁在重新定义我们的城市
2026-05-18栏目: 教程
-
阿里巴巴集团副总裁郭继军:智慧城市建设如何不变成房地产项目
2026-05-18栏目: 教程
-
阿里云RDS for SQL Server购买使用流程
2026-05-18栏目: 教程
-
阿里云智能战略与合作部刘湘雯:阿里关于创新创业服务的思考
2026-05-18栏目: 教程
