cf补题(2.20)

关于前几天的cf,c题是一个典型大数加减的变形,贴一下原题。

Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers a and b using the following algorithm:

  1. If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length.
  2. The numbers are processed from right to left (that is, from the least significant digits to the most significant).
  3. In the first step, she adds the last digit of a to the last digit of b and writes their sum in the answer.
  4. 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 = 17236 and b = 3465 Tanya adds up as follows:

  • calculates the sum of 6 + 5 = 11 and writes 11 in the answer.
  • calculates the sum of 3 + 6 = 9 and writes the result to the left side of the answer to get 911.
  • calculates the sum of 2 + 4 = 6 and writes the result to the left side of the answer to get 6911.
  • calculates the sum of 7 + 3 = 10, and writes the result to the left side of the answer to get 106911.
  • calculates the sum of 1 + 0 = 1 and writes the result to the left side of the answer and get 1106911.

As a result, she gets 1106911.

You are given two positive integers a and s. Find the number b such that by adding a and b as described above, Tanya will get s. Or determine that no suitable b exists.

Input

The first line of input data contains an integer t the number of test cases.

Each test case consists of a single line containing two positive integers aa and s separated by a space.

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 b exists, output -1.

Sample 1

Input Output
6
17236 1106911
1 5
108 112
12345 1023412
1 11
1 20
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 b that satisfies the problem statement.

贴一下代码

#include "iostream"
#include "cstring"
#include "algorithm"
#include "vector"
#include "stack"
using namespace std;
int main(){
	string a,b;
	vector  A, B, C;
	int n;
	cin >> n;
	while(n--){
		bool flag={false};
		cin >> a >> b;
		for(int i = 0; i <= a.size() - 1; i++)A.push_back(a[i] - '0');
		for(int i = 0; i <= b.size() - 1; i++)B.push_back(b[i] - '0');
		while(!B.empty()){                                                                     
			if(B.back() >= A.back()){
				C.push_back(B.back() - A.back());
				A.pop_back(),B.pop_back();
				if(A.empty()){
				while(!B.empty()){
				C.push_back(B.back());
				B.pop_back();
				}
				}
			}
			else{
				int t = B.back();
				B.pop_back();
				if(t + B.back() * 10 - A.back() > 10 || t + B.back() * 10 - A.back() < 0){
					flag = true;
					break;
				}
				C.push_back(t + B.back() * 10 - A.back());
				A.pop_back(),B.pop_back();                                                                                                                                                                                                                                                                                                                                                                                                                              
				if(A.empty()){
				while(!B.empty()){
				C.push_back(B.back());
				B.pop_back();
				}
				}
			}
		}
		if(flag || !A.empty())printf("-1");
		else{
		while(C.back() == 0)C.pop_back();
		while(!C.empty()){
			printf("%d",C.back());
			C.pop_back();
		}
		}
		printf("\n");
		C.clear(), A.clear(), B.clear();

	}
}

你可能感兴趣的:(c++)