B1016部分A+B

 正整数 A 的“DA​​(为 1 位整数)部分”定义为由 A 中所有 DA​​ 组成的新整数 PA​​。例如:给定 8,DA​​=6,则 A 的“6 部分”PA​​ 是 66,因为 A 中有 2 个 6。

现给定 A、DA​​、B、DB​​,请编写程序计算 PA​​+PB​​。

输入格式:

输入在一行中依次给出 A、DA​​、B、DB​​,中间以空格分隔,其中 0。

输出格式:

在一行中输出 PA​​+PB​​ 的值。

输入样例 1:

3862767 6 13530293 3

输出样例 1:

399
 

输入样例 2:

3862767 1 13530293 8
 

输出样例 2:

0

思路:

使用两个字符串str1,str2保存PA 和PB,然后利用stio()函数将字符串转化成整数型,相加得到结果。

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main() {
 5     string a, b, str1="0", str2="0";//初始化,使得result=0时正确输出
 6     char d1, d2;
 7     cin >> a >> d1 >> b >> d2;
 8     for (unsigned int i = 0; i < a.length(); i++) {
 9         if (a[i] == d1)
10             str1 += d1;
11     }
12     for (unsigned int i = 0; i < b.length(); i++) {
13         if (b[i] == d2)
14             str2 += d2;
15     }
16     int num1 = stoi(str1);
17     int num2 = stoi(str2);
18     int result = num1 + num2;
19     cout << result << endl;
20     return 0;
21 }

猜你喜欢

转载自www.cnblogs.com/PennyXia/p/12284445.html
今日推荐