HDOJ 1002 A + B Problem II 大整数相加高效率版的C语言实现

A + B Problem II

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


Problem Description

I have a verysimple problem for you. Given two integers A and B, your job is to calculatethe Sum of A + B.

 

Input

The firstline of the input contains an integer T(1<=T<=20) which means the numberof test cases. Then T lines follow, each line consists of two positiveintegers, A and B. Notice that the integers are very large, that means youshould not process them by using 32-bit integer. You may assume the length ofeach integer will not exceed 1000. 

 

Output

For each testcase, 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 intthe 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 


#include<stdio.h>
#include<string.h>

int main()
{
    int len_max,len_min,len_str_a,len_str_b,t,i,j,c,temp;
    char str_a[1001],str_b[1001],str_c[1001];
    char *len_max_str;
    scanf("%d",&t);
    for(j=1; j<=t; j++)
    {
        //输入两个加数的字符串并记录长度
        scanf("%s",str_a);
        len_str_a = strlen(str_a);
        scanf("%s",str_b);
        len_str_b = strlen(str_b);
        //记录最大长度、最小长度、最大长度字符串首地址
        if(len_str_a>len_str_b)
        {
            len_max = len_str_a;
            len_max_str = str_a;
            len_min = len_str_b;
        }
        else
        {
            len_max = len_str_b;
            len_max_str = str_b;
            len_min = len_str_a;
        }

        c=0; //进位初始为0
        for(i=0; i<len_max; i++) //从个位开始相加
        {
            //得到当前位相加的结果
            if(i<len_min) //如果两个数都存在该位,那么当前位的结果就是就把两数的该位以及进位数相加
            {
                temp=(str_a[len_str_a-1-i]-'0')+(str_b[len_str_b-1-i]-'0')+c;
            }
            else  //如果只有长度较长的数存在该位,那么当前位的结果就是长度较长的数该位的值加上进位数
            {
                temp = len_max_str[len_max-1-i]-'0'+c;
            }
            //记录当前位最终结果
            str_c[len_max-1-i] = temp%10+'0';
            //记录进位数
            c=temp/10;
        }
        str_c[len_max] = '\0';//给结果的字符串添加结束标志
        //输出结果
        printf("Case %d:\n%s + %s = ",j,str_a,str_b);
        if(c!=0) //如果最高位还存在进位,输出的时候就多个1(两数相加进位最多为1)
        {
            printf("1%s",str_c);
        }
        else
        {
            printf("%s",str_c);
        }
        printf("\n");
        if(j<t) //题目要求在每两个输出之间价格空行,并不是每个输出都加(最后一行不加)
        {
            printf("\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值