PAT 1016
思路:用字符串类型string可以很方便的获取题目要求的数字,获得DA||DB的个数,然后通过循环转换成整型,相加后的到结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步