目录
https://blog.csdn.net/velscode/article/details/84348774
题目信息
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.
我有一个非常简单的任务给你,给出两个整数A和B,你的工作是计算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.
第一行输入包括一个整数T,1<=T<=20,是测试用例的数量。接下来的 T 行,每行包括两个正整数A和B。注意这个整数非常大,意味着你不能用32位整型来处理他们。你可以任务每个整数的长度不会超过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.
对于每个测试用例,你应该输出两行。第一行是Case #:,#代表测试用例的序号。第二行是一个等式"A + B = Sum",Sum是A+B的结果。注意每个等式中有一些空格,每两个结果中输出一个空白行。
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
题解
思路
典型的大数相加,用字符串解决,可以参考我写的
基于字符串的大数相加_C语言实现
注意事项
每个用例输出都必须跟一个换行,另外,除了最后一个用例,其它用例还要再多一个换行,否则会报格式错。
Code
#include <stdio.h>
int T;
char A[20][2000];
char B[20][2000];
char R[20][2000];
void Big_Number_Add( char A[], char B[], char R[] );
int main()
{
int i ;
scanf("%d",&T);
for(i=0;i<T;i++)
{
scanf("%s%s",A[i],B[i]);
Big_Number_Add(A[i],B[i],R[i]);
}
for(i=0;i<T;i++)
{
printf("Case %d:\n",i+1);
printf("%s + %s = %s",A[i],B[i],R[i]);
if(i!=T-1)
printf("\n\n");
else
printf("\n");
}
return 0;
}
/**
* @name Big_Number_Add
* @brief 基于字符串的大数相加函数
* @author Velscode
* @para R[] is the string of result of A+B
*/
void Big_Number_Add( char A[], char B[], char R[] )
{
long long int len_a = 0, len_b = 0,i,j;
unsigned char carry_bit = 0, next_carry_bit = 0 ;
char * p;
//calculate length of A[]
p = A;
while( *p != '\0' )
{
p++;
len_a++;
}
//calculate length of B[]
p = B;
while( *p != '\0' )
{
p++;
len_b++;
}
if( len_a > len_b )
{
R[len_a+1] = '\0';
for( i = 0; i < len_b; i++)
{
R[len_a-i] = ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
next_carry_bit = ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) / 10;
carry_bit = next_carry_bit;
}
for( ; i < len_a; i++)
{
R[len_a-i] = ( (A[len_a-1-i] - '0') + carry_bit ) % 10 + 48;
next_carry_bit = ( (A[len_a-1-i] - '0') + carry_bit ) / 10 ;
carry_bit = next_carry_bit;
}
if( carry_bit != 0 )
R[0] = carry_bit + 48;
else
{
for(i=0;i<len_a;i++)
R[i] = R[i+1];
R[i] = '\0';
}
}
if( len_a < len_b )
{
R[len_b+1] = '\0';
for( i = 0; i < len_a; i++)
{
R[len_b-i] = ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
next_carry_bit = ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) / 10;
carry_bit = next_carry_bit;
}
for( ; i < len_b; i++)
{
R[len_b-i] = ( (B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
next_carry_bit = ( (B[len_b-1-i] - '0') + carry_bit ) / 10 ;
carry_bit = next_carry_bit;
}
if( carry_bit != 0 )
R[0] = carry_bit + 48;
else
{
for(i=0;i<len_b;i++)
R[i] = R[i+1];
R[i] = '\0';
}
}
if( len_a == len_b )
{
R[len_a+1] = '\0';
for( i = 0; i < len_a; i++)
{
R[len_b-i] = ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
next_carry_bit = ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) / 10;
carry_bit = next_carry_bit;
}
if( carry_bit != 0 )
R[0] = carry_bit + 48;
else
{
for(i=0;i<len_a;i++)
R[i] = R[i+1];
R[i] = '\0';
}
}
return ;
}