正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。现给定A、DA、B、DB,请编写程序计算PA + PB。
#include<iostream>
#include<string>
using namespace std;
int sumAandB(string strA, int DA, string strB, int DB){
int sumA = 0;
int sumB = 0;
for (int i = 0; i < strA.size(); i++){
char tmp = strA[i];
if ((tmp - '0') == DA)
sumA = sumA * 10 + (tmp - '0');
}
for (int i = 0; i < strB.size(); i++){
char tmp = strB[i];
if ((tmp - '0') == DB)
sumB = sumB * 10 + (tmp - '0');
}
return sumA + sumB;
}
int main(){
string strA, strB;
int DA, DB;
cin >> strA >> DA >> strB >> DB;
cout << sumAandB(strA, DA, strB, DB) << endl;
cin.get();
cin.get();
}
本文介绍了一个C++程序,该程序能够接收两个整数A和B及其对应的指定数字DA和DB,然后分别提取A和B中所有的DA和DB组成新的整数PA和PB,并计算PA与PB的总和。
4万+

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



