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:\n%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("\n\n");
        else 
            printf("\n");
    }
    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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM