#include<cstdio> intmain(){ longlong a, b, da, db; longlong pa = 0, pb = 0; scanf("%lld%lld%lld%lld", &a, &da, &b, &db); while(a != 0){ if(a%10 == da){ pa = pa * 10 + da; } a /= 10; } while(b != 0){ if(b%10 == db){ pb = pb * 10 +db; } b /= 10; } printf("%lld", pa + pb); return0; }
用 C++利用字符串解法
利用 C++ 中的 string 格式存储 a,b,当 da==(a[i]-‘0’) 时,即为相等,利用 string 转换为 int 型方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream> usingnamespacestd; intmain(){ string a, b; int da, db, pa = 0, pb = 0; cin >> a >> da >> b >> db; for(int i = 0; i < a.length(); i++){ if(da == (a[i]-'0')) pa = pa * 10 + da; } for(int i = 0; i< b.length(); i++){ if(db == (b[i]-'0')) pb = pb * 10 + db; } cout << pa + pb;