ACM——hdu1002(高精度加法)

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.
(第一行输入一个数字T(1 <= T <= 20)代表输入的数据组数,其后输入T行,每行包含2个欲求和的数字A和B,由于这两个数非常大,这意味着你不能用32位整数来处理它们,同时,你可以假定每个数字不超过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.
(注意输出的格式,对于每一种案例,你需要输出两行,第一行为“Case#:”,指示这是第几个测试样例,第二行为一个公式”A + B = Sum”,Sum表示A加B的结果。注意在等式中有一些空格,在两个样例间输出一空白行。)
Sample Input
2
1 2
112233445566778899 998877665544332211

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

解题思路:

  1. 首先用2个字符串存储要输入的两个加数A、B;
  2. 去掉字符串中多余的0,比如0001改为1;
  3. 将字符串转换为整型数组并倒序存储(ASCII中,‘1’-‘0’=1);
  4. 对A、B数组每一位进行加法运算,并存储在另一个数组中;
  5. 按要求输出结果;
  6. 将各个数据清0。

Submission

#include <stdio.h>
#include <string.h>
int main() {
    int m,n,i,j,k;
    char a[1000]={0};
    char b[1000]={0};
    int a1[1000]={0};
    int b1[1000]={0};
    int c[1001]={0};
    int la,lb,la1,lb1,max;
    scanf("%d",&n);
    for(m=1;m<=n;m++) 
    {
        scanf("%s %s",&a,&b);
        la=strlen(a);
        lb=strlen(b);
        for(i=0;i<la;i++){   //去掉A中的前导零 
            if(a[i]!='0' && a[i]!=NULL){
                la1=i;
                break;
            }
            else if(a[i]=='0' && i==la-1){
                la1=0;
                la=1;
            }
        }
        /*从字符串的第一个字符开始遍历,遇到'0'跳过,直到遇到字符串中第一个不为'0'的字符,若整个字符串全为'0',则遍历完整个字符串。比如字符串'00999',遍历到第一个字符'0',la1为0,到第二字符'0',la1为0,第三个字符不为'0',la1为2,那么之后字符串是从第三位开始倒序存储的,这样就清除了前导的'0'。*/
        for(i=0;i<lb;i++){ //去掉B中的前导零
            if(b[i]!='0'&&b[i]!=NULL){
                lb1=i;
                break;
            }
            else if(a[i]=='0'&&i==la-1){
                lb1=0;
                lb=1;
            }
        }
        for(j=la-1,k=0;j>=la1;j--,k++){//倒置字符串,并转换为整型数组 
            a1[k]=a[j]-'0';//ASCII码中,字符‘0’和数字0相差
        }
        for(j=lb-1,k=0;j>=lb1;j--,k++){
            b1[k]=b[j]-'0';
        }
        if(la>=lb) max=la;
        else max=lb;
        for(i=0;i<max;i++){//进行加法运算
            if(a1[i]+b1[i]+c[i]>=10){
                c[i]=a1[i]+b1[i]+c[i]-10;
                c[i+1]=c[i+1]+1;
            }
            //当有进位时,此位减10,高一位加1;无进位时,此位A、B对应的加数和进位相加。
            else{
                c[i]=a1[i]+b1[i]+c[i];
            }
        }
    printf("Case %d:\n",m);//输出结果
    for(j=la1;j<la;j++){
        printf("%c",a[j]);
    }
    printf(" + ");
    for(j=lb1;j<lb;j++){
        printf("%c",b[j]);
    }
    printf(" = ");
    if(c[max]==0) max=max-1; 
    for(j=max;j>=0;j--){
        printf("%d",c[j]);
    }
    if(m!=n){
        printf("\n\n"); 
    }
    else{
        printf("\n");
    }
    for(j=0;j<=max;j++){ //将数组清0
        a1[j]=0;
        b1[j]=0;
        c[j]=0;
    }
    }
    return 0;

}

写这道题比较糟心的地方有经过一次样例输出后忘记将数据清0,导致后面的样例全都计算错误,不过调试之后就解决啦!

猜你喜欢

转载自blog.csdn.net/marilynmontu/article/details/81153138