题目链接:Problem - C - Codeforces
Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers aa and bb using the following algorithm:
- If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length.
- The numbers are processed from right to left (that is, from the least significant digits to the most significant).
- In the first step, she adds the last digit of aa to the last digit of bb and writes their sum in the answer.
- At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer.
For example, the numbers a=17236a=17236 and b=3465b=3465 Tanya adds up as follows:
+17236034651106911+17236034651106911
- calculates the sum of 6+5=116+5=11 and writes 1111 in the answer.
- calculates the sum of 3+6=93+6=9 and writes the result to the left side of the answer to get 911911.
- calculates the sum of 2+4=62+4=6 and writes the result to the left side of the answer to get 69116911.
- calculates the sum of 7+3=107+3=10, and writes the result to the left side of the answer to get 106911106911.
- calculates the sum of 1+0=11+0=1 and writes the result to the left side of the answer and get 11069111106911.
As a result, she gets 11069111106911.
You are given two positive integers aa and ss. Find the number bb such that by adding aa and bb as described above, Tanya will get ss. Or determine that no suitable bb exists.
Input
The first line of input data contains an integer t (1≤t≤104) — the number of test cases.
Each test case consists of a single line containing two positive integers a and s (1≤a
Output
For each test case print the answer on a separate line.
If the solution exists, print a single positive integer bb. The answer must be written without leading zeros. If multiple answers exist, print any of them.
If no suitable number bb exists, output -1.
Example
input
6 17236 1106911 1 5 108 112 12345 1023412 1 11 1 20
output
3465 4 -1 90007 10 -1
Note
The first test case is explained in the main part of the statement.
In the third test case, we cannot choose bb that satisfies the problem statement.
题意:给定一个计算方法,然后给你一个数和他们的和,求出另一个数,如果没有输出-1
思路:模拟一下,如果当前位置不够减就需要把前面一位拿过来,如果大于10或者小于0,不行,如果第一个数比和长也不行
#includeusing namespace std; int main(){ int t; cin >> t; string s, a; while(t--){ cin >> a >> s; int ss = s.length() - 1; int aa = a.length() - 1; string b = ""; bool f = 0; while(ss > -1 && aa > -1){ int s1 = s[ss] - '0'; int a1 = a[aa] - '0'; if(s1 < a1){ if(ss == 0){ f = 1; break; } ss--; s1 += (s[ss] - '0') * 10; } int ans = s1 - a1; if(ans >= 10 || ans < 0){ f = 1; break; } string str = ""; str += ans + '0'; b = str + b; ss--; aa--; } if(aa != -1){ f = 1; } if(f){ cout << -1 << endl; }else{ for(ss; ss >= 0; ss--){ b = s[ss] + b; } for(int i = 0; i < b.length(); i++){ if(b[i] != '0'){ f = 1; } if(b[i] == '0' && f == 0){ b.erase(i, 1); i--; } } cout << b << endl; } } return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)