[HDOJ]1002 A + B Problem II 个人代码保留及小结

题目内容

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




AC代码

#include<stdio.h>
#include<string.h>
int main(){
    int t,i,j,re,min_len,index_a,r[1001],index_b;
    char a[1001],b[1001];

    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%s%s",a,b);
        index_a=strlen(a)-1;//a end
        index_b=strlen(b)-1;//b end 
        for(j=0;j<=(index_a>index_b?index_a:index_b)+1;j++) r[j]=0;  
        j=0;
        while(index_a>=0&&index_b>=0)
        {
            re=a[index_a--]+b[index_b--]-2*'0'; 
            if(re>=10)
            {
                r[j++]+=(re%10); r[j]++; 
            }
            else//重点在这里 很容易忽略 1+9=10的情况 
            {
                r[j++]+=re;
                if(r[j-1]>=10)
                {
                    r[j-1]=r[j-1]%10;
                    r[j]++;
                }
            }
        }
        if(index_a>=0)
        {
            while(index_a+1)// end when index_a=-1
            {
                r[j++]+=a[index_a--]-'0';
            }
        }
        else if(index_b>=0)
        {
            while(index_b+1)// end when index_b=-1
                {
                    r[j++]+=b[index_b--]-'0';
                }
        }
        printf("Case %d:\n",i+1);
        printf("%s + %s = ",a,b);
        if(r[j]==0) j--;
        while(j+1) printf("%d",r[j--]);
        printf("\n");
        if(i<t-1) printf("\n");     
    }
    return 0;   
} 

总结:
这道题因为我的基础知识不扎实导致了几个严重的bug
首先第一个 scanf(“%s”,a); 在读取字符时如果是数字 12345 最后会按索引存入 12345五个数字,也就是说,如果我同时读入两个数组 两个长度不一样的数字,就会出现一种状况
index:0 1 2 3 4

扫描二维码关注公众号,回复: 2713809 查看本文章

a: 1 2 3 4 5

b: 1 2
可以看到,如果要把a和b按加法进行计算的话就会出现重要的一点 需要按index从末尾开始相加
即 r[0]=a[strlen(a)-1]+b[strlen(b)-1]-2*’0’;
那么,问题就在这里,这是我第一个出现的错误;

第二个致命,也是花费时间最长的bug十分隐蔽,错因是由于自己的数学逻辑还是不够完善
想象一下,如果两个数字相加得到的re(result)=9会出现什么状况
因为判断条件为re<10的话就直接 r[j]+re
然而,r[j]可能已经在之前的步骤中获得了进一的1;
那么此时 re=9 9+1=10 也就是有一个数字为10 这显然不满足我们的要求
所以最妥当的办法是这样的,
先让r[j]+re 判断此时的值与10的大小 即 先计算后判断
若此时值大于等于10 取余 高位进1即可
小于10 ,不用管
这也是一个思维问题,当你提前让后一个数发生改变时,必须要考虑可能出现的所有情况
先计算 后判断 或许在计算机的计算中也是十分重要的一环

猜你喜欢

转载自blog.csdn.net/liuHuaWei5909/article/details/81486058