Wrong Addition

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:

  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 aa to the last digit of bb 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 = 17236a=17236 and b = 3465b=3465 Tanya adds up as follows:

\large{ \begin{array}{r} + \begin{array}{r} 17236\\ 03465\\ \end{array} \\ \hline \begin{array}{r} 1106911 \end{array} \end{array}}+1723603465​1106911​​​

  • 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 tt (1 \le t \le 10^41≤t≤104) — the number of test cases.

Each test case consists of a single line containing two positive integers aa and ss (1 \le a \lt s \le 10^{18}1≤a<s≤1018) 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 bb exists, output -1.

Sample 1

InputcopyOutputcopy
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 bb that satisfies the problem statement.

无脑翻译: 

Tanya正在学习如何添加数字,但到目前为止,她做得不正确。她正在添加两个数字一个一个和bb使用以下算法:

  1. 如果其中一个数字比另一个数字短,Tanya 将前导零相加,使数字长度相同。
  2. 这些数字从右到左(即,从最低有效数字到最高有效数字)进行处理。
  3. 在第一步中,她将一个一个到 的最后一位数字bb并在答案中写下他们的总和。
  4. 在下一步中,她对同一位置的每对数字执行相同的操作,并将结果写入答案的左侧

例如,数字a = 17236一个=17236和b = 3465b=3465谭雅加起来如下:

\large{ \begin{array}{r} + \begin{array}{r} 17236\\ 03465\\ \end{array} \\ \hline \begin{array}{r} 1106911 \end{array} \end{array}}+1723603465​1106911​​​

  • 计算6 + 5 = 116+5=11并写入1111在答案中。
  • 计算3 + 6 = 103+6=9并将结果写入答案的左侧以获得911911.
  • 计算2 + 4 = 62+4=6并将结果写入答案的左侧以获得69116911.
  • 计算7 + 3 = 107+3=10,并将结果写入答案的左侧以获得106911106911.
  • 计算1 + 0 = 11+0=1并将结果写到答案的左侧并得到11069111106911.

结果,她得到了11069111106911.

您将获得两个正整数一个一个和ss.查找号码bb这样,通过添加一个一个和bb如上所述,谭雅会得到ss.或者确定没有合适的bb存在。

输入

输入数据的第一行包含一个整数tt (1 \le t \le 10^41≤t≤104) — 测试用例的数量。

每个测试用例由包含两个正整数的一行组成一个一个和ss (1 \le a \lt s \le 10^{18}1≤一个<s≤1018) 由空格分隔。

输出

对于每个测试用例,将答案打印在单独的行上。

如果解决方案存在,请打印单个正整数bb.答案必须不带前导零。如果存在多个答案,请打印其中任何一个。

如果没有合适的数字bb存在,输出 -1。

示例 1

输入复制输出复制
6
17236 1106911
1 5
108 112
12345 1023412
1 11
1 20
3465
4
-1
90007
10
-1

注意

第一个测试用例在语句的主要部分中进行了解释。

在第三个测试用例中,我们无法选择bb满足问题陈述。

这道题主要是有比较多的情况要考虑到,当时做的时候也是一点一点发现漏洞的

在做出来之后补充了详细的注释。

 

#include<bits/stdc++.h>
using namespace std;
int n,l1,l2,ans,flag;
///用到的两个c++函数。
///从前面插入:s.insert (0,"111");
///int转string:to_string(i);
string s,sum,b;

//去除前导0
string fun(string s)
{
    int cnt=0;
    for(char c:s)
    {
        if(c!='0')
        {
            break;
        }
        else if(c=='0')
        {
            cnt++;
        }
    }
    return s.substr(cnt,s.size()-cnt);
}
int main()
{
    cin>>n;
    while(n--)
    {
        flag=0;
        b="";
        cin>>s>>sum;
        ///情况1:s和sum相等时,应返回0
        if(s==sum)
        {
            cout<<"0"<<endl;
            continue;
        }
        l1=s.size()-1,l2=sum.size()-1;
        while(l2>=0)///只要sum还没遍历完,就要继续循环
        {
            int i;
            if(l1>=0)///情况2:因为s和所求b我们不知道哪个更长,所以如果s没了,sum还有,就不加s位置模拟补0
            {
                i=sum[l2]-'0'-(s[l1]-'0');
            }
            else///这里就是s模拟补0
            {
                i=sum[l2]-'0';
            }

            if(i>=0)///情况3:如果两个数字相减和大于等于0,就直接加入到答案字符串里
            {
                b.insert(0,to_string(i));
            }
            else if(i<0 && l2>=1)///如果两个数字相减小于0说明sum得加一位,即加上前一个数字作为十位数,但前提是sum的下标还大于等于1
            {
                l2--;
                i+=(sum[l2]-'0')*10;//将新的一位作为十位数
                if(i>=10)///如果得到b是两位数,直接宣告失败,因为按照规则,s和b是一位一位相加的
                {
                    flag=-1;
                    break;
                }
                else if(i>=0 &&i<10)///如果得到的b是个位数,则满足条件,加到答案字符串里去
                {
                    b.insert(0,to_string(i));
                }
                else if(i<0)///如果sum进了一位得到的b还小于0,这是不可能的,那就宣告失败。
                {
                    flag=-1;
                    break;
                }

            }
            else if(i<0 && l2<1)///如果sum不足以再进位了,说明无法满足,直接宣布失败
            {
                flag=-1;
                break;
            }
            l1--,l2--;
        }

        if(l1>=0 ||l2>=0 || flag==-1)//失败输出-1
        {
            cout<<"-1"<<endl;
        }
        else//成功输出去掉前导0的答案字符串b
        {
            cout<<fun(b)<<endl;
        }
    }
    return 0;
}

警醒!!!!!!!!!!!!!!!!!!

不要每次c++做题的时候,需要什么就去查函数拿来用就是了

遇到一个就学一个,好好记住,不要每次就查一下用了就是,记不住他们。

这样子永远也学不会的。

经常看看之前整理的c++方法!!

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值