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

Positive integer A of "D * A ** (as an integer) portion" is defined as A * all * D ** A * new compositions integer P A . For example: Given A = 3,862,767, D A = 6, then A "section 6" of the * P ** A * 66, because A has two 6.

Now given A , D A , B , D B , write a program to calculate P A + P B .

Input formats:

In the given input line are sequentially A , D A , B , D B , separated by a space, where 0 < A , B <1010.

Output formats:

Output line P A + P B value.

Sample Input 1:
3862767 6 13530293 3
Output Sample 1:
399
Sample Input 2:
3862767 1 13530293 8
Output Sample 2:
0
Thinking
  • Actually deal with the problem of string, like a number of traversing number again
  • Use stringstream, to save himself wrote convert ~
Code
#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;
}
Quote

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

Guess you like

Origin www.cnblogs.com/MartinLwx/p/11614123.html
Recommended