ACM1002:A + B Problem II

Posted mycodinglife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ACM1002:A + B Problem II相关的知识,希望对你有一定的参考价值。

Problem Description
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
--------------------------------------------------------------------------------------------------------------------------
 
//高精度加法,注意格式
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<memory.h>
#define SIZE 1005

int main()
{
	int i, n;
	int j, k, m;
	int index;
	int lengthA, lengthB;
	char a[SIZE];
	char b[SIZE];

	int ans[SIZE];
	int carryBit;    //进位
	//freopen("F:\input.txt","r",stdin);
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	{
		scanf("%s%s", a, b);

		memset(ans, 0, sizeof(ans));
		index = 0;
		carryBit = 0;
		lengthA = strlen(a);
		lengthB = strlen(b);

		lengthA--;
		lengthB--;
		while ((lengthA >= 0) && (lengthB >= 0))
		{
			//将char转换为int,并计算和,如果ans大于10,则carryBit为1,否则为0
			ans[index] = (a[lengthA] - ‘0‘) + (b[lengthB] - ‘0‘) + carryBit;
			if (ans[index] >= 10)
			{
				carryBit = ans[index] / 10;
				ans[index] %= 10;
			}
			else
				carryBit = 0;
			index++;
			lengthA--;
			lengthB--;
		}
		//如果A的长度大于B,则将A与carryBit相加
		while (lengthA >= 0)
		{
			ans[index] = (a[lengthA] - ‘0‘) + carryBit;
			if (ans[index] >= 10)
			{
				carryBit = ans[index] / 10;
				ans[index] %= 10;
			}
			else
				carryBit = 0;
			index++;
			lengthA--;
		}
		//如果B的长度大于A,则将B与carryBit相加
		while (lengthB >= 0)
		{
			ans[index] = (b[lengthB] - ‘0‘) + carryBit;
			if (ans[index] >= 10)
			{
				carryBit = ans[index] / 10;
				ans[index] %= 10;
			}
			else
				carryBit = 0;
			index++;
			lengthB--;
		}

		if (carryBit != 0)
		{
			ans[index]++;
			index++;
		}

		//去掉多余的零
		while (ans[index] == 0)
			index--;
		printf("Case %d:
", i + 1);
		printf("%s + %s = ", a, b);
		//两个零字符串相加输出为0
		if (index == -1)
			printf("0");
		else
		{
			for (m = index; m >= 0; m--)
				printf("%d", ans[m]);
		}
		printf("
");
		if (i != n - 1)
			printf("
");
	}
	//freopen("con", "r", stdin);
	//system("pause");
	return 0;
}

  

以上是关于ACM1002:A + B Problem II的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1002 A - A + B Problem II (大数问题)

HDU1002 A + B Problem II 大数问题

A + B Problem II

大数加法 (A + B Problem II)

大数运算 A + B Problem II

1002: A+B for Input-Output Practice (II)