PAT (Basic Level) Practice (Chinese) 1016
1016 Part 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 = 3862767, 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
Ideas:
Digital calculation is repeated string meaning of the questions and the new string can be configured according Da Db.
Code:
#include <iostream>
#include <string>
using namespace std;
int inWath(string s,char k) {
int n = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == k)
n++;
}
return n;
}
int main() {
string a, b;
char Da,Db;
string Pa,Pb;
cin >> a >> Da >> b >> Db;
if (inWath(a, Da) != 0 && inWath(b, Db) != 0)
cout << stoi(Pa.insert(0, inWath(a, Da), Da)) + stoi(Pb.insert(0, inWath(b, Db), Db));
else
cout << "0";
return 0;
}