hdu 1002 A + B Problem II(大数相加)
2017-06-28 15:32
676 查看
本题链接:点击打开链接
Total Submission(s): 362459 Accepted Submission(s): 70479
[align=left]Problem Description[/align]
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 2
112233445566778899 998877665544332211
[align=left]Sample Output[/align]
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
[align=left]Author[/align]
Ignatius.L
本题题意:先输入一个数 t,表示有 t 组测试数据库,然后给出两个数,注意的是这两个数很大,甚至超出长整型的范围,然后计算这两个数的和,输出格式是先输出Case #:其中 ‘#’ 表示第几组测试数据,然后接下来一行输出结果。每组测试数据之后再空一行,最后一组末尾没有空行。
解题思路:大数相加,一般都是用数组先存起来,然后分别取数组最后一位相加,若相加大于10,就用p值矫正进位,若其中一个数组用完,就直接记录该值就好,最后再倒序输出即可。
代码1:
代码2(分步计算):
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 362459 Accepted Submission(s): 70479
[align=left]Problem Description[/align]
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 2
112233445566778899 998877665544332211
[align=left]Sample Output[/align]
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
[align=left]Author[/align]
Ignatius.L
本题题意:先输入一个数 t,表示有 t 组测试数据库,然后给出两个数,注意的是这两个数很大,甚至超出长整型的范围,然后计算这两个数的和,输出格式是先输出Case #:其中 ‘#’ 表示第几组测试数据,然后接下来一行输出结果。每组测试数据之后再空一行,最后一组末尾没有空行。
解题思路:大数相加,一般都是用数组先存起来,然后分别取数组最后一位相加,若相加大于10,就用p值矫正进位,若其中一个数组用完,就直接记录该值就好,最后再倒序输出即可。
代码1:
#include <stdio.h> #include <string.h> int main() { char s1[1005],s2[1005], s[1005];//s数组存为和值 int x=1; int t; int len1,len2,k; scanf("%d",&t); while(t--) { int p=0; scanf("%s %s",s1,s2); printf("Case %d:\n",x++); len1=strlen(s1)-1; //第一个数的长度 len2=strlen(s2)-1; //第二个数的长度 for(k=0; len1>=0|| len2>=0; len1--, len2--,k++) //当s1和s2数组长度均为0时退出循环,都从最后一位开始相加 { if( len1>=0&& len2>=0) s[k]=s1[len1]+s2[ len2]-'0'+p; if( len1<0&& len2>=0) s[k]=s2[len2]+p; //第一个数据 if( len1>=0&& len2<0) s[k]=s1[len1]+p; p=0; //记得还原 if(s[k]>'9') { s[k]=s[k]-10; p=1; //该位相加大于十就进位 } } printf("%s + %s = ",s1,s2); if(p==1) //最后的数大于十就先输出进位1 printf("1"); while(k--) //倒序输出 printf("%c",s[k]); if(t!=0) printf("\n\n"); else printf("\n"); } return 0; }
代码2(分步计算):
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { int t,x=1,i,j,k,lena,lenb,p; char a[20000],b[20000]; scanf("%d",&n); while(t--) { int s[20000]= {0}; printf("Case %d:\n",x++); scanf("%s%s",a,b); lena=strlen(a); lenb=strlen(b); printf("%s + %s = ",a,b); j=lena-1; k=lenb-1; p=0; while(j>=0&&k>=0) //当数组a和数组其中一个数组的长度为0时退出 { if(s[p]+(a[j]-'0')+(b[k]-'0')>=10) //进位 { s[p]=s[p]+(a[j]-'0')+(b[k]-'0')-10; s[p+1]++;//数组s的之后的一位加一 } else s[p]=s[p]+(a[j]-'0')+(b[k]-'0'); //否则直接相加 p++; k--; j--; } //执行完前面的代码之后必定只剩下一个数组的值了 if(j>=0) { for(i=j; i>=0; i--) { s[p]=s[p]+(a[i]-'0'); p++; } } else if(k>=0) { for(i=k; i>=0; i--) { s[p]=s[p]+b[i]-'0'; p++; } } while(p--) printf("%d",s[p]);//倒序输出 printf("\n"); if(t!=0) printf("\n"); } return 0; }
相关文章推荐
- HDU 1002:A + B Problem II(大数相加)
- HDU 1002 A + B Problem II 大数相加
- HDU 1002 A + B Problem II(两个大数相加)
- 【每日一题(19)】A + B Problem II(大数相加) HDU - 1002
- HDU 1002 A + B Problem II 【大数相加】(3.16)
- HDU 1002 A + B Problem II 大数相加
- B - A + B Problem II HDU - 1002 (大数相加问题)
- hdu1002 A + B Problem II(大数相加)
- (大数相加)HDU 1002 A + B Problem II
- hdu 1002 A + B Problem II 大数相加
- HDU 1002 A + B Problem II 大数相加
- HDU 1002 A+B Problem II 大数相加
- hdu 1002 A + B Problem II(大数相加)
- HDU 1002 A + B Problem II(大数相加)
- HDU 1002 - A + B Problem II (大数相加)
- hdu1002 A + B Problem II(大数题)
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
- HDOJ 1002 A + B Problem II(大数相加)
- 杭电 HDU 1002 A + B Problem II Java 大数的初级用法
- hdu 1002 A + B Problem II(Java大数)