hduA + B Problem II

    技术2024-08-18  66

    A + B Problem II

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 71329    Accepted Submission(s): 13022 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 2 1 2 112233445566778899 998877665544332211   Sample Output Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110   Author Ignatius.L

     

     

     

    大年初一无聊了,刷刷题目吧,HDU的1002,最水的大数。。。。。。。

     

    #include<cstdio> #include<cstring> #define MAX 1005 #define mod 10000 #define baselen 4 #define in(a) scanf("%d",&a) #define out1(a) printf("%d",a) #define out2(a) printf("%04d",a) typedef int type; struct bint { type dig[MAX], len; bint() { len = 0, dig[0] = 0; } }; //常用函数 void add(bint a, bint b, bint& c) //(1) { type i, carry ; for( i = carry = 0; i <= a.len || i <= b.len || carry; i++) { if(i<=a.len)carry += a.dig[i]; if(i<=b.len)carry += b.dig[i]; c.dig[i] = carry%mod; carry /= mod; } c.len = i - 1; } bool input(bint& a) //(6) { type i, j, w, k, p; char data[MAX*baselen+1]; if(scanf("%s",data)==EOF)return false; w = strlen(data) - 1, a.len = 0; for(p=0; p<=w&&data[p]=='0'; p++); while(1) { i = j = 0, k = 1; while(i<baselen&&w>=p) { j = j+ (data[w--] - '0')*k; k *= 10, i++; } a.dig[a.len++] = j; if(w<p)break; } a.len--; return true; } void output(bint& a) //(7) { type i; i = a.len - 1; out1(a.dig[a.len]); while(i>=0)out2(a.dig[i--]); } // int main() { int T; scanf("%d",&T); int cas=1; while(T--) { bint a,b,c; if(cas>1) printf("/n"); input(a); input(b); printf("Case %d:/n",cas++); output(a); printf(" + "); output(b); printf(" = "); add(a,b,c); output(c); printf("/n"); } return 0; }  

    最新回复(0)