思路:用字符串类型string可以很方便的获取题目要求的数字,获得DA||DB的个数,然后通过循环转换成整型,相加后的到结果
#include<iostream> using namespace std; int main() { string A = "0" , B = "0"; int DA = 0, DB = 0; cin >> A >> DA >> B >> DB; int count_A = 0, count_B = 0; int side = 0; side = 11; if (A.length() >=side|| B.length() >=side) { return 0; } for (int i = 0; i < A.length(); i++) { //获取DA的个数 if (A[i] == (DA+'0')) { count_A++; } } for (int i = 0; i < B.length(); i++) { //获取DB的个数 if (B[i] == (DB + '0')) { count_B++; } } int sum_A = 0, sum_B = 0, sum = 0; while (count_A--) { //转换成整数 sum_A *= 10; sum_A += DA; } while (count_B--) { sum_B *= 10; sum_B += DB; } cout << sum_A + sum_B << endl; return 0; }