当前位置:网站首页>Wrong Addition
Wrong Addition
2022-07-26 07:28:00 【STATICHIT静砸】
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:
\large{ \begin{array}{r} + \begin{array}{r} 17236\\ 03465\\ \end{array} \\ \hline \begin{array}{r} 1106911 \end{array} \end{array}}+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 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
Inputcopy Outputcopy 6 17236 1106911 1 5 108 112 12345 1023412 1 11 1 20 3465 4 -1 90007 10 -1Note
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使用以下算法:
- 如果其中一个数字比另一个数字短,Tanya 将前导零相加,使数字长度相同。
- 这些数字从右到左(即,从最低有效数字到最高有效数字)进行处理。
- 在第一步中,她将一个一个到 的最后一位数字bb并在答案中写下他们的总和。
- 在下一步中,她对同一位置的每对数字执行相同的操作,并将结果写入答案的左侧。
例如,数字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}}+17236034651106911
- 计算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++方法!!






边栏推荐
- MySQL安装教程-手把手教你安装
- Download and install the free version of typora
- 此章节用于补充
- Compose text and icon splicing to realize drawableleft or drawableright
- PostgreSQL UUID fuzzy search UUID string type conversion SQL error [42883] explicit type casts
- NFT digital collection development: Six differences between digital collections and NFT
- NFT数字藏品开发:数字艺术藏品赋能公益平台
- 【推荐系统经典论文(十)】阿里SDM模型
- 2019 ZTE touyue · model compression scheme
- keras学习之:获取神经网络中间层的输出结果
猜你喜欢

NFT digital collection system development: activating digital cultural heritage

数据平台调度升级改造 | 从Azkaban 平滑过度到 Apache DolphinScheduler 的操作实践

NLP natural language processing - Introduction to machine learning and natural language processing (3)

What is bloom filter in redis series?

Countdown 2 days! Based on the cross development practice of Apache dolphin scheduler & tidb, you can greatly improve your efficiency from writing to scheduling

6、MySQL数据库的备份与恢复

Learn browser decoding from XSS payload

Hcip--- BGP comprehensive experiment

NFT digital collection development: digital collections help enterprise development
![[daily question 1] 919. Complete binary tree inserter](/img/a2/2ff77ccdfb78bb1812b989342c2331.png)
[daily question 1] 919. Complete binary tree inserter
随机推荐
Typora免费版下载安装
3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力
Compose Canvas line chart
PostgreSQL UUID fuzzy search UUID string type conversion SQL error [42883] explicit type casts
Uncover the mystery of cloud native data management: operation level
PXE efficient batch network installation
QT: list box, table, tree control
DCN (deep cross network) Trilogy
OVS底层实现原理
NFT digital collection development: digital art collection enabling public welfare platform
Download and install the free version of typora
[daily question 1] 919. Complete binary tree inserter
How to expand and repartition the C disk?
NFT digital collection system development: how enterprises develop their own digital collection platform
ModuleNotFoundError: No module named ‘pip‘解决办法
Taishan office lecture: word error about inconsistent values of page margins
NFT digital collection development: Six differences between digital collections and NFT
正则表达式规则以及常用的正则表达式
NFT digital collection system development: the collision of literature + Digital Collections
Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程