【PAT 1016B】部分A+B

1016 部分A+B (15分)

正整数 A 的“D​A​​(为 1 位整数)部分”定义为由 A 中所有 D​A​​ 组成的新整数 P​A​​。例如:给定 A=3862767,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<A,B<10​10​​。

输出格式:

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

输入样例 1:

3862767 6 13530293 3

输出样例 1:

399

输入样例 2:

3862767 1 13530293 8

输出样例 2:

0

code

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
	unsigned int A,B=0;
	int d1=0,d2=0;
	cin>>A>>d1>>B>>d2;
	string a=to_string(A);
	string b=to_string(B);
	int cnt1=1,cnt2=1;
	for (int i = 0; i < a.size(); ++i)
	{
		if (a[i]-'0'==d1)
		{
			cnt1++;
		}
	}
	for (int i = 0; i < b.size(); ++i)
	{
		if (b[i]-'0'==d2)
		{
			cnt2++;
		}
	}
	int ans1=0,ans2=0;
	while(cnt1--)
	{
		ans1+=pow(10,cnt1-1)*d1;
	}
	while(cnt2--)
	{
		ans2+=pow(10,cnt2-1)*d2;
	}
	//cout<<ans1<<" "<<ans2<<endl;
	cout<<ans1+ans2<<endl;
	return 0;

}
发布了199 篇原创文章 · 获赞 62 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/IT_flying625/article/details/104139206