1016. 部分A+B (15)(版本二)

正整数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 < 1010。

输出格式:

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

输入样例1:
3862767 6 13530293 3
输出样例1:
399
输入样例2:
3862767 1 13530293 8
输出样例2:
0
记得好像之前好像写过这一题,但是好像写得比较麻烦,这个解法感觉比较简单。

#include<bits/stdc++.h>
using namespace std;
int n1,m1,n2,m2,s1,s2,t;
int main()
{
    cin>>n1>>m1>>n2>>m2;
    while(n1)
    {
        t=n1%10;
        if(t==m1)
            s1=s1*10+t;
            n1/=10;
    }
    while(n2)
    {
        t=n2%10;
        if(t==m2)
            s2=s2*10+t;
            n2/=10;
    }
    cout<<s1+s2;
    return 0;

}
发布了218 篇原创文章 · 获赞 24 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/Wanglinlin_bfcx/article/details/78554663