HDOJ1002 A+B Problem II(高精度/大整数加法)
2017-08-17 14:57
288 查看
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 369581 Accepted Submission(s): 72050
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
Author
Ignatius.L
【分析】注意到输入的数字A和B最高可达1000位,因此考虑使用字符数组(即字符串)保存输入的数字。计算时:
(1)可先将A和B逆序,便于从低位向高位逐位相加
(2)A和B可能有3种情况:A和B位数相等,A比B大,A比B小。逐位相加时随时记录相加结果和低位向当前为的进位情况【需要特别注意数字与字符间的转接问题,+/- '0'】
(3)(2)步骤结束后,有可能最高位仍产生进位,此时需要将进位作为结果的最高位
(4)将结果串逆序,得到最终结果
#include <stdio.h>
#include <string.h>
int T;
int ncase=1;
char num1[1010],num2[1010],ret[1010];
void Reverse(char *str)
{
int i=0,j=strlen(str)-1;
char temp;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++,j--;
}
}
void BigAdd(char *n1,char *n2,char *ret)
{
int i,j;
int acc,temp,digit;
int len1=strlen(n1);
int len2=strlen(n2);
Reverse(n1);
Reverse(n2);
i=0,j=0,acc=0,digit=0;
while(i<len1 && j<len2) //A和B的"公共部分"
{
temp=n1[i]-'0'+n2[j]-'0'+acc;
ret[digit++]=(temp%10)+'0';
acc=temp/10;
i++,j++;
}
while(i<len1) //A比B大
{
temp=n1[i]-'0'+acc;
ret[digit++]=(temp%10)+'0';
acc=temp/10;
i++;
}
while(j<len2) //A比B小
{
temp=n2[j]-'0'+acc;
ret[digit++]=(temp%10)+'0';
acc=temp/10;
j++;
}
if(acc==1) //保存最高位产生的进位
ret[digit++]=acc+'0';
ret[digit]='\0';
Reverse(ret);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%s %s",num1,num2);
printf("Case %d:\n",ncase++);
printf("%s + %s = ",num1,num2);
BigAdd(num1,num2,ret);
printf("%s\n",ret);
if(T!=0)
printf("\n");
}
return 0;
}
相关文章推荐
- HDOJ 1002 A + B Problem II 高精度的A+B问题
- HDOJ 1002 A + B Problem II 大整数相加高效率版的C语言实现
- hdoj 1002 A + B Problem II【大数加法】
- 【hdoj1002】 A + B Problem II(模拟加法)
- 大整数加法 1002 A + B Problem II
- HDOJ1002 A + B Problem II(大数加法)
- hdoj 1002 A + B Problem II(大数加法)
- hdoj 1002 A + B Problem II 【大数加法模板】
- HDU 1002 A + B Problem II 高精度加法程序 覆盖大量字符串处理基础 详细注释
- HDU 1002 A + B Problem II 大整数加法
- [HDOJ1002] A + B Problem II 高数度加法运算
- Hdu 1002 A + B Problem II(大整数加法)
- hdoj 1002 A + B Problem II 高精度 java
- HDOJ-1002 A + B Problem II 高精度(java)
- HDOJ1002 A + B Problem II
- 1002—A + B Problem II(高精度)
- HDU 1002 A + B Problem II(大数加法,C,Java两个版本)
- HDOJ 1002 A + B Problem II【大数,基础】
- HDOJ 1002丶 ( A + B Problem II )
- HDOJ1002 A + B Problem II