当前位置:网站首页>Wrong Addition
Wrong Addition
2022-07-26 07:29:00 【Statichit static smash】
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.
Brainless translation :
Tanya Learning how to add numbers , But so far , She didn't do it right . She is adding two numbers, one One and bb Use the following algorithm :
- If one number is shorter than the other ,Tanya Add leading zeros , Make the numbers the same length .
- These numbers go from right to left ( namely , From the least significant number to the most significant number ) To deal with .
- In the first step , She will have one One To The last digit of bb And write their total in the answer .
- In the next step , She performs the same operation on each pair of numbers in the same position , And write the result into the answer left .
for example , Numbers a = 17236 One =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
- Calculation 6 + 5 = 116+5=11 And write 1111 In the answer .
- Calculation 3 + 6 = 103+6=9 And write the result to the left of the answer to get 911911.
- Calculation 2 + 4 = 62+4=6 And write the result to the left of the answer to get 69116911.
- Calculation 7 + 3 = 107+3=10, And write the result to the left of the answer to get 106911106911.
- Calculation 1 + 0 = 11+0=1 Write the result to the left of the answer and get 11069111106911.
result , She got 11069111106911.
You will get two positive integers, one One and ss. Find number bb such , By adding a One and bb As mentioned above , Tanya will get ss. Or make sure there is no suitable bb There is .
Input
The first row 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 line containing two positive integers One and ss (1 \le a \lt s \le 10^{18}1≤ One <s≤1018) Separated by spaces .
Output
For each test case , Print the answer on a separate line .
If the solution exists , Please print a single positive integer bb. The answer must be without leading zeros . If there are multiple answers , Please print any of them .
If there is no appropriate number bb There is , Output -1.
Example 1
Input Copy Output Copy 6 17236 1106911 1 5 108 112 12345 1023412 1 11 1 20 3465 4 -1 90007 10 -1Be careful
The first test case is explained in the main part of the statement .
In the third test case , We can't choose bb Meet the problem statement .
There are many situations to consider in this problem , At that time, loopholes were found bit by bit
After it is made, detailed notes are added .
#include<bits/stdc++.h>
using namespace std;
int n,l1,l2,ans,flag;
/// Two used c++ function .
/// Insert... From the front :s.insert (0,"111");
///int turn string:to_string(i);
string s,sum,b;
// Remove the lead 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;
/// situation 1:s and sum When equal , Should return 0
if(s==sum)
{
cout<<"0"<<endl;
continue;
}
l1=s.size()-1,l2=sum.size()-1;
while(l2>=0)/// as long as sum It's not over yet , Just keep cycling
{
int i;
if(l1>=0)/// situation 2: because s And what you ask b We don't know which is longer , So if s be without ,sum also , No more s Position simulation supplement 0
{
i=sum[l2]-'0'-(s[l1]-'0');
}
else/// Here is the s Analog complement 0
{
i=sum[l2]-'0';
}
if(i>=0)/// situation 3: If the sum of two numbers is greater than or equal to 0, Just add it directly to the answer string
{
b.insert(0,to_string(i));
}
else if(i<0 && l2>=1)/// If the subtraction of two numbers is less than 0 explain sum You have to add one , That is, add the previous number as ten digits , But the premise is sum The subscript of is greater than or equal to 1
{
l2--;
i+=(sum[l2]-'0')*10;// Take the new one as ten digits
if(i>=10)/// If you get b It's two digits , Directly declare failure , Because according to the rules ,s and b It's a bit by bit addition
{
flag=-1;
break;
}
else if(i>=0 &&i<10)/// If you get b It's a single digit , Then the conditions are met , Add it to the answer string
{
b.insert(0,to_string(i));
}
else if(i<0)/// If sum Got into a get b Less than 0, It's impossible , Then declare failure .
{
flag=-1;
break;
}
}
else if(i<0 && l2<1)/// If sum Not enough to carry , Explain that it is not possible to meet , Declare failure directly
{
flag=-1;
break;
}
l1--,l2--;
}
if(l1>=0 ||l2>=0 || flag==-1)// Failure output -1
{
cout<<"-1"<<endl;
}
else// Successfully output without leading 0 Answer string b
{
cout<<fun(b)<<endl;
}
}
return 0;
}Vigilance !!!!!!!!!!!!!!!!!!
Not every time c++ When you do the problem , If you need anything, just check the function and use it
Learn from every one you meet , Remember , Don't check it every time , Can't remember them .
You'll never learn like this .
Often look at what I've done before c++ Method !!






边栏推荐
- 依赖和关联的对比和区别
- QT: modal, modeless, text box, button, single line input box
- Hcip--- BGP comprehensive experiment
- 程序环境和预处理
- PostgreSQL UUID fuzzy search UUID string type conversion SQL error [42883] explicit type casts
- Hcip--- MPLS detailed explanation and BGP route filtering
- C语言关键字extern
- Countdown 2 days! Based on the cross development practice of Apache dolphin scheduler & tidb, you can greatly improve your efficiency from writing to scheduling
- 5. Multi table query
- What is bloom filter in redis series?
猜你喜欢
![Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]](/img/21/c35b1493a36c46a89db0c6ebcf46db.jpg)
Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]

单身杯web wp

Network Trimming: A Data-Driven Neuron Pruning Approach towards Efficient Deep Architectures论文翻译/笔记

DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network

正则表达式规则以及常用的正则表达式

在线问题反馈模块实战(十四):实现在线答疑功能

PXE efficient batch network installation

6. Backup and recovery of MySQL database

Singles cup web WP

Pycharm common shortcut keys
随机推荐
NFT数字藏品系统开发:华为发布首款珍藏版数字藏品
PXE efficient batch network installation
WCF 部署在IIS上
What is bloom filter in redis series?
How to convert multi row data into multi column data in MySQL
NLP natural language processing - Introduction to machine learning and natural language processing (3)
程序环境和预处理
ARIMA model for time series analysis and prediction
Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程
[200 opencv routines] 231. Gray level co-occurrence matrix (GLCM) for feature description
HOT100 hash
IDEA快捷键
NFT digital collection system development: what are the best digital marketing strategies for NFT digital collection
博途PLC一阶滞后系统传递函数阶跃响应输出仿真(SCL)
Open source management system based on ThinkPHP
【C语言】你真的了解printf吗?(printf典型易错,强烈建议收藏)
NFT digital collection development: digital collections help enterprise development
PostgreSQL sequence create alter nextval Curval numerical interval gap
【每日一题】919. 完全二叉树插入器
China Unicom transformed the Apache dolphin scheduler resource center to realize the one-stop access of cross cluster call and data script of billing environment