码迷,mamicode.com
首页 > 其他好文 > 详细

B1016部分A+B

时间:2020-02-08 19:19:49      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:signed   std   ensure   name   names   unsigned   nbsp   stream   baseline   

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

现给定 A、D?A??、B、D?B??,请编写程序计算 P?A??+P?B??。

输入格式:

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

输出格式:

在一行中输出 P?A??+P?B?? 的值。

输入样例 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 }

 

B1016部分A+B

标签:signed   std   ensure   name   names   unsigned   nbsp   stream   baseline   

原文地址:https://www.cnblogs.com/PennyXia/p/12284445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!