本文主要是介绍NYOJ-103-A+B Problem II-2013年08月16日23:56:46,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
A+B Problem II
时间限制: 3000 ms | 内存限制: 65535 KB
难度: 3
- 描述
-
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 must be positive.
- 输入
- 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. 输出
- 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. 样例输入
-
2 1 2 112233445566778899 998877665544332211
样例输出 -
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
# include<stdio.h>
# include<string.h>int main()
{int T,i,j,k;char a[1200],b[1200];int a1[1200],b1[1200];scanf("%d",&T);for(i=1;i<=T;i++){getchar();scanf("%s%s",a,b);memset( a1, 0, sizeof(a1));memset( b1, 0, sizeof(b1));for(j=0,k=strlen(a)-1;k>=0;k--)a1[j++] = a[k] - '0';for(j=0,k=strlen(b)-1;k>=0;k--)b1[j++] = b[k] - '0';printf("Case %d:\n",i);for(j=1100;j>0;j--){if(a1[j]!=0){j++;break;}}if(j==0){printf("%d",a1[0]);}else{if(a1[j]==0)j--;for(;j>=0;j--){printf("%d",a1[j]);}}printf(" + ");for(j=1100;j>0;j--){if(b1[j]!=0){j++;break;}}if(j==0){printf("%d",b1[0]);}else{if(b1[j]==0)j--;for(;j>=0;j--){printf("%d",b1[j]);}}for(j=0;j<1200;j++){a1[j] = a1[j] + b1[j];if(a1[j] >= 10){a1[j] = a1[j] - 10;a1[j+1]++;}}printf(" = ");for(j=1100;j>0;j--){if(a1[j]!=0){j++;break;}}if(j==0){printf("%d",a1[0]);}else{if(a1[j]==0)j--;for(;j>=0;j--){printf("%d",a1[j]);}}printf("\n");if(i<T)printf("\n");}return 0;
}
这篇关于NYOJ-103-A+B Problem II-2013年08月16日23:56:46的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!