部分A+B

    正整數A的“DA(爲1位整數)部分”定義爲由A中所有DA組成的新整數PA。例如:給定A = 3862767,DA = 6,則A的“6部分”PA是66,因爲A中有2個6。現給定A、DA、B、DB,請編寫程序計算PA + PB

   

#include<iostream>
#include<string>
using namespace std;


int sumAandB(string strA, int DA, string strB, int DB){
	int sumA = 0;
	int sumB = 0;
	for (int i = 0; i < strA.size(); i++){
		char tmp = strA[i];
		if ((tmp - '0') == DA)
			sumA = sumA * 10 + (tmp - '0');
	}
	for (int i = 0; i < strB.size(); i++){
		char tmp = strB[i];
		if ((tmp - '0') == DB)
			sumB = sumB * 10 + (tmp - '0');
	}
	return sumA + sumB;
}

int main(){
	string strA, strB;
	int DA, DB;
	cin >> strA >> DA >> strB >> DB;
	cout << sumAandB(strA, DA, strB, DB) << endl;
	cin.get();
	cin.get();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章