PAT_1016_部分A+B

该博客主要讨论PAT(A+B)问题的解法,涉及大数存储与计算。作者通过使用字符数组存储超过long long范围的大数,并通过计数法实现大数相加。博客中提到,设置数组尺寸为10000可以成功通过测试,但设置为题目所给的最大尺寸会导致编译错误,这是由于编译器对数组尺寸的限制。博客内容适合对编程竞赛和大数处理感兴趣的读者。
摘要由CSDN通过智能技术生成

PAT_1016_部分A+B

题目比较简单,注意的点只是如何存储这么大的数。原题A、B的范围都是0-10^10,用long long 不太够,用字符数组存储即可。

有个疑问,为什么我设置数组尺寸为10000都可以通过,如果设置题中上界的尺寸,编译器会报错,说尺寸太大了。这个题这点有点蒙。

#include <iostream>
#include <cstdio>
#include <string.h>

using namespace std;
const unsigned int maxsize = 10000;

int main()
{
    char A[maxsize], B[maxsize];
    int Da, Db, Pa = 0, Pb = 0;
    scanf("%s", A);
    int Alen = strlen(A);
    scanf("%d", &Da);
    scanf("%s", B);
    int Blen = strlen(B);
    scanf("%d", &Db);

    int cnt1 = 0, cnt2 = 0;
    for(int i = 0; i < Alen; i++){
        if(Da == A[i] - '0'){
            cnt1++;
        }
    }
    for(int i = 0; i < Blen; i++){
        if(Db == B[i] - '0'){
            cnt2++;
        }
    }

    while(cnt1--){
        Pa = Pa * 10 + Da;
    }

    while(cnt2--){
        Pb = Pb * 10 + Db;
    }

    printf("%d\n", Pa+Pb);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值