CF 1619C - Wrong Addition

文章描述了一个名为Tanya的女孩使用特定的加法算法,即对齐数字并从右向左逐位相加。当数字长度不同时,她会在较短的数字前面补零。给定两个正整数a和s,任务是找到另一个正整数b,使得按照Tanya的算法,a和b相加得到s。题目提供了几个例子并要求程序解决此类问题,输出可能的b值或在无解时输出-1。
摘要由CSDN通过智能技术生成

C. Wrong Addition

Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers a a a and b b 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 a a to the last digit of b b 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 a=17236 a=17236 and b = 3465 b=3465 b=3465 Tanya adds up as follows:
例子

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

As a result, she gets 1106911 1106911 1106911.
 
You are given two positive integers a a a and s s s. Find the number b b b such that by adding a a a and b b b as described above, Tanya will get s s s. Or determine that no suitable b b b exists.
 
Input
The first line of input data contains an integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) — the number of test cases.
Each test case consists of a single line containing two positive integers a a a and s s s ( 1 ≤ a < s ≤ 10 1≤a<s≤10 1a<s1018) 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 b b. The answer must be written without leading zeros. If multiple answers exist, print any of them.
If no suitable number b b b exists, output − 1 -1 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 b b b that satisfies the problem statement.

题目大意:
s s s 是由 a a a b b b 的每一对应数位相加之后的结果组合而组成的(对应数位不存在的用0加)。我们要做的是反过来,由题目给出的 s s s a a a 求出 b b b
解题思路:
由题意可以得出, a a a b b b 的每一位都是 0 ~ 9 的数,也就是说 s s s 中的每一或两位数减去 a a a 的一位数只能得出 b b b 的其中一位数,即差为 0 ~ 9,若超出范围就不存在合适的 b b b 了。
从右往左进行计算,分别获取 s s s a a a 的一位数,若 s s s取的一位数 比 a a a的一位数小,则 s s s 再取一位数,然后进行相减(s的减a的)。若差值在 0 ~ 9 则认为是正确的并进行组合,若不在这个范围,则认为不存在这样的 b b b (差值小于0的: s s s 不够减且 s s s 没有向左多一位或左一位为0,此时 b b b 为负数;差值大于9的:此时 b b b 的其中一部分为两位数)。综上,若有这样的 b b b 则进行输出,若没有则输出 − 1 -1 1

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    int t; cin>>t;
    while (t--)
    {
        ll a, s; cin>>a>>s;
        ll ta, ts, t=1;
        ll b = 0;
        bool flag = true; //是否 b 的每一位数都满足要求
        while(a || s) // s 的每(1或2)位和 a 的每一位都要进行一次计算
        {
            //获取 a 和 s 的低位数
            ta = a%10; a /= 10;
            ts = s%10; s /= 10;
            if(ts < ta) //若 s 不能减去 a 则再多取一位
            {
                ts += s%10*10;
                s /= 10;
            }
            //获取差值
            int tb = ts - ta;
            //差值在 0~9 范围内才是符合 b 每一位数的要求的
            //否则就认为不存在合适的 b
            if(tb<0 || tb>9)
            {
                flag = false;
                break;
            }
            b += tb*t;
            t *= 10;
        }
        cout<<(flag?b:-1)<<'\n';
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花生ono

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值