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;
}
收藏 打印