oj 1002题 (大数题)
Posted yzbpxx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oj 1002题 (大数题)相关的知识,希望对你有一定的参考价值。
#include <stdio.h> #include <string.h> int main(void) { int q,j,h,k,l; int d; char s1[1001],s2[1001];//题目要求位数不大于1000 scanf("%d",&h); for(l=1;l<=h;l++){ int c[1001]={0}, a[1001]={0},b[1001]={0};//这样可以令数组内全部元素为0 scanf("%s %s",&s1,&s2); int cd1,cd2,cd3,cdmax,cdmin; cd1=strlen(s1); cd2=strlen(s2); cdmax=cd1>cd2?cd1:cd2; for(q=0,j=cd1;q<cd1;q++,j--) a[q]=s1[j-1]-48; for(q=0,j=cd2;q<cd2;q++,j--) b[q]=s2[j-1]-48; for(k=0,d=0;k<cdmax;k++) { c[k]=(a[k]+b[k]+d)%10; //个个位相加满十进一 d=(a[k]+b[k]+d)/10; } printf("Case %d: %s +%s =",l,s1,s2); if(d!=0){//判断最后一位和是否大于一 c[cdmax]=1; for(cd3=cdmax;cd3>=0;cd3--){ printf("%d",c[cd3]); } } else{ for(cd3=cdmax-1;cd3>=0;cd3--){ printf("%d",c[cd3]); } } if(l!=h)//最后跳一行 printf(" "); else printf(" "); } return 0; }
题目:
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.
处理 1000以内 ,即使double longlong 也无法满足
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
方法步骤:将数字以字符串的方式存储,在单个位相加(用10取余),满十进一(除10),注意初始化(对前几位数赋值,后面直接默认赋0)
以上是关于oj 1002题 (大数题)的主要内容,如果未能解决你的问题,请参考以下文章