其他分享
首页 > 其他分享> > Codeforces C. Wrong Addition

Codeforces C. Wrong Addition

作者:互联网

Codeforces Round #762 (Div. 3)

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=17236 and b=3465 Tanya adds up as follows:

As a result, she gets 1106911.

You are given two positive integers aa and ss. 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 (1≤t≤10000) — the number of test cases.

Each test case consists of a single line containing two positive integers aa and ss (1≤a<s≤10^18) 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 b. The answer must be written without leading zeros. If multiple answers exist, print any of them.

If no suitable number b 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.

 

 这道题应该要拆开各个数位上的数分别来算,以下称各个位数为a[i],b[i],s[j]。

我们不难发现,有的a[i]+b[i]后为两位数,有的为一位数,其中和为一位数得到的s[j]>=a[i],和为两位数的s[j-1]*10+s[j]<a[i],我们可以依据这个思路来写代码。

其中还有两种非法的输入使得输入后无法得到相应的b:

 

 现在上代码。

#include <stdio.h>
#include <string.h>
int main(){
    char a[30],s[30],b[30];
    int t; 
    scanf("%d",&t);//读入重复的次数。
    while(t--){
        memset(a,0,sizeof(a));//对字符串进行初始化。
        memset(b,0,sizeof(b));
        memset(s,0,sizeof(s));
        scanf("%s %s",a,s);
        int la=strlen(a);
        int ls=strlen(s);
        int i=la-1,j=ls-1,k=0,flag=1;//i、j分别是a、s的末位元素序号,flag判断输入是否合法。
        while(i>=0){
                if(a[i]>s[j]){          //这种情况是a[i]+b[i]>=10。
                    if(s[j-1]!='1'){    //判断是否合法。
                        flag=0;
                        break;
                    }if(flag==0)break;
                    b[k]=s[j]-a[i]+10;  
                    i--;
                    j-=2;
                    k++;
                }else if(a[i]<=s[j]){   //这种情况是a[i]+b[i]<10。
                    b[k]=s[j]-a[i];    
                    i--;
                    j--;
                    k++;
                }
                if(j<0&&i>=0){          //判断是否合法。
                    flag=0;
                    break;
                }
        }
        k=k-1;
        if(flag==0)printf("-1\n");  
        if(flag==1){
            if(j==-1){  
              //此时i也等于-1,这种情况是进行多次这种操作后,a、s的元素恰好都用完。
                while(b[k]==0){     //去除前导0
                    k--;
                }
                for(k;k>=0;k--){        // 逆序输出b。
                    printf("%d",b[k]);
                }
                printf("\n");
            }else {     
              //这种情况是进行多次这种操作后,a的元素合法肯定用完,s的元素还有剩余。
                for(int v=0;v<=j;v++){  //先把s的剩余元素正序输出。
                    printf("%c",s[v]);
                }
                for(;k>=0;k--){         //再倒序输出b。
                    printf("%d",b[k]);
                }
                printf("\n");
            }
        }
    }
    return 0;
}

标签:Addition,writes,flag,Codeforces,int,Wrong,result,answer,sum
来源: https://blog.csdn.net/m0_61978392/article/details/122059449