PAT-B 1016 部分A+B
https://pintia.cn/problem-sets/994805260223102976/problems/994805306310115328
题目
正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA。例如:给定 A=3862767,DA=6,则 A 的“6 部分”PA 是 66,因为 A 中有 2 个 6。现给定 A、DA、B、DB,请编写程序计算 PA+PB。
输入
输入在一行中依次给出 A、DA、B、DB,中间以空格分隔,其中 0<A,B<10^10。
输出
在一行中输出 PA+PB 的值。
样例输入
3862767 6 13530293 3
样例输出
399
分析
用字符串接收数字,统计相应部分数字的个数,最后进行计算。具体看程序。
C++程序
#include<iostream>
using namespace std;
typedef long long ll;
const ll p[]={0,1,11,111,1111,11111,111111,1111111,11111111,111111111,1111111111};
int main()
{
int a,b,k1=0,k2=0;
string s1,s2;
cin>>s1>>a>>s2>>b;
for(int i=0;i<s1.length();i++)
if(s1[i]-'0'==a)
k1++;
for(int i=0;i<s2.length();i++)
if(s2[i]-'0'==b)
k2++;
ll ans=a*p[k1]+b*p[k2];
cout<<ans<<endl;
return 0;
}