编程题目: PAT 1016. 部分A+B (15)

1016. 部分A+B (15)

时间限制
100 ms
内存限制
32000 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

正整数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
        连着做了两个麻烦一点的题目,终于又遇到一个简单的。没啥特别的,直接上代码。

/*
http://pat.zju.edu.cn/contests/pat-b-practise/1016
*/
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1,str2;
	char ch1,ch2;
	int sum1=0,sum2=0,sum3=0;
	int count1 = 1,count2 =1;
	cin>>str1>>ch1>>str2>>ch2;
	for(int i=0;i<str1.length();i++)
	{	
		if(str1[i]==ch1)
		{
			sum1+= count1*(ch1-48);
			count1*=10;
		}
	}
	for(int i=0;i<str2.length();i++)
	{	
		if(str2[i]==ch2)
		{
			sum2+= count2*(ch2-48);
			count2*=10;
		}
	}
	cout<<sum1+sum2<<endl;
	system("pause");
	return 0;
}




posted @ 2014-07-03 16:11  F8Master  阅读(330)  评论(0编辑  收藏  举报