This is an A+B Problem SDUT 2613

题目描述

As usual, there will be an A+B problem in warming up, this problem is:
Given two integers A and B, your job is to calculate the sum of A + B.

输入

  Th ere are several test cases, For each test case:
There are two integers A, B for each case (0  A , B < 101000).

输出

  For each test case, output one line containing the result of A+B. 

示例输入

1 2
11111111111 11111111111

示例输出

3
22222222222

大数模拟,应该不难理解
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

int main()
{
    char a[1510],b[1510];
    int c[1510];
    while(scanf("%s%s",a,b)!=EOF)
    {
        memset(c,0,sizeof(c));
        int i;
        int k1=strlen(a)-1;
        int k2=strlen(b)-1;
        int  p=k1>k2?k1:k2;

        for(i=0; k1>=0; i++,k1--)
        {
            c[i]=a[k1]-'0';
        }
        for(i=0; k2>=0; i++,k2--)//只有k2次循环,如果最后一次大于10,便无法进位,所以有了下面的while循环
        {
            c[i]+=b[k2]-'0';
            if(c[i]>=10)
            {

                c[i+1]++;
                c[i]-=10;
            }
        }

        while(c[i]!=0)
        {
            if(c[i]>=10)
            {
                c[i+1]++;
                c[i]-=10;
            }
            i++;
        }
        if(c[p+1]!=0)//两个数中大的数的最高位进位,字符串长度加1,所以如果不为0,输出的就是真正的最高位。
        {
            printf("%d",c[p+1]);
        }
        for(p; p>=0; p--)
        {
            printf("%d",c[p]);
        }
        printf("\n");

    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值