題目描述
正整數A的“DA(為1位整數)部分”定義為由A中所有DA組成的新整數PA。例如:給定A = 3862767,DA = 6,則A的“6部分”PA是66,因為A中有2個6。
現給定A、DA、B、DB,請編寫程式計算PA + PB。
輸入
輸入在一行中依次給出A、DA、B、DB,中間以空格分隔,其中0 < A, B < 1010。
輸出
在一行中輸出PA + PB的值。
樣例輸入
3862767 6 13530293 3
3862767 1 13530293 8
樣例輸出
399
代碼
#include <cstdio>
#define maxn 10
int main () {
char A[maxn]={0}, B[maxn]={0}; //用字元串方法求得要比較數字的個數
long long int a, b, Pa=0, Pb=0; //Pa Pb為變化後的數字
while (scanf ("%s %d %s %d", A, &a, B, &b)!=EOF) {
for (int i=0; A[i]!='\0'; i++) {//字元串以'\n'作為結束的标志
if (A[i]-'0' == a) { //字元串操作表示整型數字
Pa = Pa*10 + a ;
}
}
for (int i=0; B[i]!='\0'; i++) {
if (B[i]-'0' == b) {
Pb = Pb*10 + b ;
}
}
printf ("%lld\n", Pa + Pb) ; //直接輸出兩個長整型數字的和
}
return 0 ;
}