码迷,mamicode.com
首页 > 其他好文 > 详细

ACM1002:A + B Problem II

时间:2019-03-10 22:21:51      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:har   input   ons   sum   des   tom   nes   out   content   

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:\n", 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("\n");
		if (i != n - 1)
			printf("\n");
	}
	//freopen("con", "r", stdin);
	//system("pause");
	return 0;
}

  

ACM1002:A + B Problem II

标签:har   input   ons   sum   des   tom   nes   out   content   

原文地址:https://www.cnblogs.com/mycodinglife/p/10507568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!