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;
}