HDU A + B Problem II

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 189142    Accepted Submission(s): 36144


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
 

Recommend
We have carefully selected several similar problems for you:  1004 1003 1008 1005 1089 


解题思路:
一开始,我想找到可以直接输入输出长度为1000位的方法。。。。可惜没有,没办法,只能将数字视为字符一位一位的存在字符数组里面,
然后将两个字符数组从后面开始一位一位转换为数字相加,大于等于10的本位减去1,前一位加1,然后就很easy了:
wa代码:(不知道why)
#include<stdio.h>
#include<string.h>
int main()
{
    char dashu1[1050],dashu2[1050],str;
    int n,i,j,m,k,t,length1,length2;
    int a[1300]={0};//数组随着运行数据在改变,可是我没有归零~~
    scanf("%d",&n);
    m=n;
    while(n--){


        printf("Case %d:\n",m-n);//输出过早~~
        scanf("%s%s",dashu1,dashu2);
        length1=strlen(dashu1);
        length2=strlen(dashu2);
        printf("%s + %s = ",dashu1,dashu2);   
        
      if(length1<length2){                                            //用了复杂的处理,可是逻辑出错
                t=length2;
            for(i=length1-1,j=length2-1;i>=0;i--,j--)
            {
                k=dashu1[i]+dashu2[j]-'0'-'0'+a[j];
                if(k>=10){
                    k-=10;
                    a[j-1]+=1;
                }
                a[j]=k;
            }
        }
        else{t=length1;
             for(i=length2-1,j=length1-1;i>=0;i--,j--)
            {
                k=dashu1[i]+dashu2[j]-'0'-'0'+a[j];
                if(k>=10){
                    k-=10;
                    a[j-1]+=1;
                }
                a[j]=k;
        }
        }
            if(a[0]>=10){
                a[0]-=10;
                printf("1");
            }
for(i=0;i<t;i++)
            printf("%d",a[i]);//~~~漏了输出换行~~~~
           if(n)
           printf("\n");
        }
        
     return 0;
    }
    


AC代码:
#include<stdio.h>
#include<string.h>


char dashu1[1010],dashu2[1020],str;


int main()
{
    int n,i,j,m,k,t,length1,length2;
    int a[1100]= {0};
    scanf("%d",&n);
    m=n;
    while(n--)
    {
        memset(a,0,sizeof(a));//一种高效的对数组按位归零的方法,头文件为#include<string.h>
        scanf("%s %s",dashu1,dashu2);
        length1=strlen(dashu1);
        length2=strlen(dashu2);
        printf("Case %d:\n",m-n);
        printf("%s + %s = ",dashu1,dashu2);


        if(length1<length2)
        {
            t=length2;
            for(i=0; i<length2; i++)
                a[i]=dashu2[i]-'0';
            for(i=length1-1,j=length2-1; j>=length2-length1; i--,j--)
                a[j]+=dashu1[i]-'0';
        }
        else
        {
            t=length1;
            for(i=0; i<length1; i++)
                a[i]=dashu1[i]-'0';
            for(i=length2-1,j=length1-1; j>=length1-length2; i--,j--)
                a[j]+=dashu2[i]-'0';
        }
        for(i=t-1; i>0; i--)
            if(a[i]>=10)
            {
                a[i]-=10;
                a[i-1]+=1;
            }
        if(a[0]>=10)
        {
            a[0]-=10;
            printf("1");
        }
        for(i=0; i<t; i++)
            printf("%d",a[i]);
            printf("\n");
        if(n) printf("\n");


    }
    return 0;
}   

总之~~~~终于AC了~~~
 
 

posted on 2014-02-27 09:59  胖胖的乓乓  阅读(144)  评论(0编辑  收藏  举报

导航