题目:
正整数 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<109。
输出格式:
在一行中输出 PA+PB 的值。
输入样例 1:
3862767 6 13530293 3
输出样例 1:
399
输入样例 2:
3862767 1 13530293 8
输出样例 2:
0
题解:
#include <bits/stdc++.h>
using namespace std;
int main(){
int A,DA,B,DB;
cin>>A>>DA>>B>>DB;
string strA=to_string(A);
string strB=to_string(B);
string strDA=to_string(DA);
string strDB=to_string(DB);
int indexA=0,indexB=0,num1=0,num2=0;
for(int i=0;i<strA.length();i++){
if(strA[i]==strDA[0]) {
if(indexA==0) indexA++;
else strDA+=strDA[0];
}
}
for(int i=0;i<strB.length();i++){
if(strB[i]==strDB[0]) {
if(indexB==0) indexB++;
else strDB+=strDB[0];
}
}
if(indexA==0) num1=0;
else num1=stoi(strDA);
if(indexB==0) num2=0;
else num2=stoi(strDB);
cout<<num1+num2<<endl;
return 0;
}

362

被折叠的 条评论
为什么被折叠?



