PTA(Basic Level)1016.部分A+B

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

现给定 ADABDB,请编写程序计算 PA+PB

输入格式:

输入在一行中依次给出 ADABDB,中间以空格分隔,其中 0<A,B<1010。

输出格式:

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

输入样例 1:
3862767 6 13530293 3
输出样例 1:
399
输入样例 2:
3862767 1 13530293 8
输出样例 2:
0
思路
  • 实际上就是字符串的处理的问题,遍历一遍数个数就好了
  • 使用了stringstream,省得自己写转换~
代码
#include<bits/stdc++.h>
using namespace std;
int get_num(string s, int x)
{
    string ans = "";
    for(int i=0;i<s.size();i++)
    {
        int num = s[i] - '0';
        if(num == x)
            ans += (char)(x + '0');
    }
    stringstream ss;
    int value = 0;
    ss << ans;
    ss >> value;
    return value;
}
int main()
{
    string a, b;
    int da, db;
    cin >> a >> da >> b >> db;
    cout << get_num(a,da) + get_num(b,db);
    return 0;
}
引用

https://pintia.cn/problem-sets/994805260223102976/problems/994805306310115328

猜你喜欢

转载自www.cnblogs.com/MartinLwx/p/11614123.html