B. Sum of Two Numbers
https://codeforces.com/problemset/problem/1788/B
思路
计算原数的所有位置上的digit和
得到digit和的一半
对于原数从左到右切分,前半部分digit和为half
例如:
1331
-->>
1300
0031
Code
https://codeforces.com/problemset/submission/1788/193134592
int t; vector<int> digits; vector<int> bdigits; vector<int> sdigits; void storedigits(int num){ while(num){ int r = num%10; digits.push_back(r); num /= 10; } reverse(digits.begin(), digits.end()); } int getds(int num){ int sum = 0; while(num){ int r = num%10; sum += r; num /= 10; } return sum; } void split(int half){ int dlen = digits.size(); int sum = 0; REP(i, dlen){ int one = digits[i]; if (sum < half){ if(sum + one <= half){ bdigits.push_back(one); sdigits.push_back(0); } else { int bpart = half - sum; int spart = one - bpart; bdigits.push_back(bpart); sdigits.push_back(spart); } } else{ bdigits.push_back(0); sdigits.push_back(one); } sum += one; } } int tonum(vector<int>& digits){ int dlen = digits.size(); int pow = 1; int sum = 0; REP(i, dlen){ int one = digits[dlen-i-1]; sum += one*pow; pow *= 10; } return sum; } int printdigits(vector<int>& digits){ int dlen = digits.size(); REP(i, dlen){ int one = digits[i]; cout << one << " "; } cout << endl; } int main() { cin >> t; REP(i, t){ int n; cin >> n; digits.clear(); storedigits(n); // printdigits(digits); int ds = getds(n); int half = ds/2; bdigits.clear(); sdigits.clear(); split(half); // printdigits(bdigits); // printdigits(sdigits); int bnum = tonum(bdigits); int snum = tonum(sdigits); cout << bnum << " " << snum << endl; } return 0; }
标签:digits,10,dlen,int,Sum,Two,num,Numbers,sum From: https://www.cnblogs.com/lightsong/p/17111052.html