HDUOJ 1002大数相加
发布时间:2020-12-14 02:27:38 所属栏目:大数据 来源:网络整理
导读:HDUOJ 1002大数相加 A + B Problem II Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 260766????Accepted Submission(s): 50426 Problem Description I have a very simple problem for you.
HDUOJ 1002大数相加 A + B Problem IITime Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
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
?
Sample Output
My solution: /*2015.7.24*/ /*大数加法基本思路:先把大数当字符串数入,再将对应的每个字符的ASCLL码减去0的ASCLL码,既得有效数字,再把得到的每个数字按序存入整型数组里*/ /*在把字符转化为数字时,对字符按倒序操作,从而确保低位在前,高位在后,在进行加法运算时,方便进位*/ #include<stdio.h> #include<string.h> #define MAX 1100 int a[MAX],b[MAX]; char a1[MAX],b1[MAX]; int main() { int i,j,n,t1,t2,k; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s%s",a1,b1); //scanf("%s",a1); memset(a,sizeof(a)); /*函数功能:把数组全部元素赋上相同的值,这里全部赋值为0*/ memset(b,sizeof(b)); /*a为数组名,0为要赋的值,sizeof(a)为该数组的总长度*/ t1=strlen(a1); t2=strlen(b1); k=0; for(j=t1-1;j>=0;j--) a[k++]=a1[j]-'0'; k=0; for(j=t2-1;j>=0;j--) b[k++]=b1[j]-'0'; for(j=0;j<t1||j<t2;j++) { a[j]+=b[j]; if(a[j]>=10) { a[j]-=10; a[j+1]+=1; } } printf("Case %d:n",i+1); printf("%s +",a1); printf(" %s = ",b1); for(;j>=0&&a[j]==0;j--)/*去除整数前的无效数字0,如:000123,前面三个零无用,便不再输出*/ if(j>=0) /*千万不要忘了for语句后的分号,作用是:当满足情况时不进行其它操作,即执行空语句*/ for(;j>=0;j--) printf("%d",a[j]); else printf("0"); printf("n"); /*输出完每组数据后,都要换行*/ if(i!=n-1) printf("n"); /*注意该题格式,最后一组输出数据的后面不输出空行*/ } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |