HDOJ 1002 A + B Problem II 大整数相加高效率版的C语言实现
2015-05-27 21:44
399 查看
A + B Problem II
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 255206 Accepted Submission(s): 49245[/b]
Problem Description
I have a verysimple problem for you. Given two integers A and B, your job is to calculatethe Sum of A + B.
Input
The firstline of the input contains an integer T(1<=T<=20) which means the numberof test cases. Then T lines follow, each line consists of two positiveintegers, A and B. Notice that the integers are very large, that means youshould not process
them by using 32-bit integer. You may assume the length ofeach integer will not exceed 1000.
Output
For each testcase, 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 intthe 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
#include<stdio.h> #include<string.h> int main() { int len_max,len_min,len_str_a,len_str_b,t,i,j,c,temp; char str_a[1001],str_b[1001],str_c[1001]; char *len_max_str; scanf("%d",&t); for(j=1; j<=t; j++) { //输入两个加数的字符串并记录长度 scanf("%s",str_a); len_str_a = strlen(str_a); scanf("%s",str_b); len_str_b = strlen(str_b); //记录最大长度、最小长度、最大长度字符串首地址 if(len_str_a>len_str_b) { len_max = len_str_a; len_max_str = str_a; len_min = len_str_b; } else { len_max = len_str_b; len_max_str = str_b; len_min = len_str_a; } c=0; //进位初始为0 for(i=0; i<len_max; i++) //从个位开始相加 { //得到当前位相加的结果 if(i<len_min) //如果两个数都存在该位,那么当前位的结果就是就把两数的该位以及进位数相加 { temp=(str_a[len_str_a-1-i]-'0')+(str_b[len_str_b-1-i]-'0')+c; } else //如果只有长度较长的数存在该位,那么当前位的结果就是长度较长的数该位的值加上进位数 { temp = len_max_str[len_max-1-i]-'0'+c; } //记录当前位最终结果 str_c[len_max-1-i] = temp%10+'0'; //记录进位数 c=temp/10; } str_c[len_max] = '\0';//给结果的字符串添加结束标志 //输出结果 printf("Case %d:\n%s + %s = ",j,str_a,str_b); if(c!=0) //如果最高位还存在进位,输出的时候就多个1(两数相加进位最多为1) { printf("1%s",str_c); } else { printf("%s",str_c); } printf("\n"); if(j<t) //题目要求在每两个输出之间价格空行,并不是每个输出都加(最后一行不加) { printf("\n"); } } return 0; }
相关文章推荐
- hdoj1002(A+B problem II 大数相加 c语言
- HDOJ 1001 Sum Problem 累加求和的高效率版的C语言实现
- hdu 1002 A + B Problem II(大正整数相加)
- HDOJ 1002 A + B Problem II(大数相加)
- hdu 1002 A + B Problem II(大正整数相加)
- HDOJ1002 A+B Problem II(高精度/大整数加法)
- HDOJ 1002 A + B Problem II ( 大数相加)
- HDOJ1002题A + B Problem II,2个大数相加
- HDOJ1002题A + B Problem II,2个大数相加
- ACM: A + B Problem II (两个大整数相加)
- 杭电ACM OJ 1002 A + B Problem II 大数问题 长整数问题
- HDU 1002 - A + B Problem II (大数相加)
- HDU 1002 A + B Problem II 【大数相加】(3.16)
- hdoj 1002 sum problem(超大数相加)
- HDOJ1002 A + B Problem II(大数加法)
- hdu 1002 A + B Problem II 大数相加
- HDU 1002 A + B Problem II 大数相加
- 【杭电-oj】-1002-A + B Problem II(大数相加)
- HDOJ A + B Problem II 大数相加问题
- HDOJ 1002 A + B Problem II