J - Bored Three-God(输出前导0的大数A+B)

本文介绍了如何解决J - Bored Three-God问题,即对两个大数进行相加,并在输出时保留前导0。题目要求在10^10000范围内的大数相加,重点在于处理可能存在的前导0。解决方案是修改现有的大数求和代码,确保在输出时不会忽略前导0。
摘要由CSDN通过智能技术生成

J - Bored Three-God

NBUT - 1228

The bored Three-God get another boring question.

This problem is ask you plus two big nubmer, please help him, when you solve this problem you can

speak to Three-God,“Oh, Please give me a diffculte one, this one is too easy”.

Input

There are muti-case
For each case, there are two integers, n, m (0 < n, m < 10^10000).

Output

Calculate two integers’ sum

Sample Input

1 1
2 3
10000 100000

Sample Output

2
5
110000

Hint

题意:

​ 大数 A + B A+B A+B

思路:

​ 不过这个输入可能有前导 0 0 0,但是输出时前导 0 0 0不能省略。把bin神的大数板子中 m o d mod mod改为 10 10 10即可

代码:

#include<queue>
#include<iostream>
#include<string.h>
#include<cstdio>
#define mset(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
const int maxn=1e5+10;
struct bigNum
{
    const static int mod=10;
    const static int DLEN=1;
    int a[maxn],len;
    bigNum()
    {
        mset(a,0),len=0;
    }
    bigNum(const char s[])
    {
        mset(a,0);
        int L=strlen(s);
        len=L/DLEN;
        if(L%DLEN) len++;
        int index=0;
        for(int i=L-1;i>=0;i-=DLEN){
            int t=0;
            int k=i-DLEN+1;;
            if(k<0) k=0;
            for(int j=k;j<=i;++j)
                t=t*10+s[j]-'0';
            a[index++]=t;
        }

    }
    bigNum operator +(const bigNum &b)const
    {
        bigNum res;
        res.len=max(len,b.len);
        for(int i=0;i<res.len;++i)
            res.a[i]=0;
        for(int i=0;i<res.len;i++){
            res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
            res.a[i+1]+=res.a[i]/mod;
            res.a[i]%=mod;
        }
        if(res.a[res.len]>0)    res.len++;
        return res;
    }
    void print(){
    printf("%d",a[len-1]);
    for(int i=len-2;i>=0;--i)
        printf("%d",a[i]);
    puts("");
    }
};
char sa[maxn],sb[maxn];
int main()
{
    bigNum res;
    while(~scanf("%s %s",sa,sb)){
        bigNum aa(sa);
        bigNum bb(sb);
        (aa+bb).print();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值