一、题目:
B. Sum of Two Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The sum of digits of a non-negative integer a is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of 123123 is 66 and the sum of digits of 1010 is 11. In a formal way, the sum of digits of a=∑i=0∞ai⋅10i=∑=0∞⋅10, where 0≤ai≤90≤≤9, is defined as ∑i=0∞ai∑=0
Given an integer n, find two non-negative integers x and y which satisfy the following conditions.
- x+y=n+=, and
- the sum of digits of x and the sum of digits of y differ by at most 11.
It can be shown that such xand y always exist.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100001≤≤10000).
Each test case consists of a single integer n (1≤n≤1091≤≤109)
Output
For each test case, print two integers x and y.
If there are multiple answers, print any.
Example
input
Copy
5 1 161 67 1206 19output
Copy
1 0 67 94 60 7 1138 68 14 5Note
In the second test case, the sum of digits of 6767 and the sum of digits of 9494 are both 1313.
In the third test case, the sum of digits of 6060 is 66, and the sum of digits of 77 is 77.
二、思路:
拆分每一数,偶数上下分得相同,奇数大的先分到下面,下一位奇数时大的分到上面。
三、代码:
#include<iostream>
using namespace std;
char a[2000000];
int main()
{
int t;
cin >> t;
while (t--) {
cin >> a;
int x = 0, y = 0, flat = 0;
for (int i = 0; a[i] != '\0'; i++) {
if (a[i] % 2 == 0) {
x = x * 10 + (a[i] - '0') / 2;
y = y * 10 + (a[i] - '0') / 2;
}
else {
if (flat == 0) {
x = x * 10 + (a[i] - '0') / 2;
y = y * 10 + (a[i] - '0') / 2 + 1;
flat = 1;
}
else {
if (flat == 1) {
x = x * 10 + (a[i] - '0') / 2 + 1;
y = y * 10 + (a[i] - '0') / 2;
flat = 0;
}
}
}
}
cout << x << " " << y << endl;
}
return 0;
}