(乙)1016. 部分A+B

来源:互联网 发布:福州淘宝美工培训 编辑:程序博客网 时间:2024/05/22 10:43

题目:http://www.patest.cn/contests/pat-b-practise/1016

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

分析:这题应该比较简单,没有什么技术含量。。


AC代码:

#include<iostream>using namespace std;int newnum(const string,const char);int main(){    ios::sync_with_stdio(false);    string A,B;    char DA,DB;    cin>>A>>DA>>B>>DB;    cout<<newnum(A,DA)+newnum(B,DB)<<endl;    return 0;}int newnum(const string a,const char b){    int result=0;    for(int i=0;i<a.length();i++)        if(a[i]==b)            result = result*10 + b - '0';    return result;}


0 0
原创粉丝点击