当前位置:网站首页>High precision subtraction
High precision subtraction
2022-07-05 05:20:00 【hunziHang】
#include<bits/stdc++.h>
using namespace std;
int compare(string s1,string s2)
{
int i;
if(s1.length()>s2.length())
return 0;
else if(s1.length()<s2.length())
return 1;
else
{
for(i=0;i<=s1.length();i++)
{
if(s1[i]>s2[i])
return 0;
else if(s1[i]<s2[i])
return 1;
}
}
return 0;
}
int main()
{
string s1,s2;
int a[100000],b[100000],i,j;
cin>>s1>>s2;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
a[0]=s1.length();
b[0]=s2.length();
for(i=1;i<=a[0];i++)
a[i]=s1[a[0]-i]-'0';
for(i=1;i<=b[0];i++)
b[i]=s2[b[0]-i]-'0';
if(compare(s1,s2)==0)
{
for(i=1;i<=a[0];i++)
{
a[i]-=b[i];
if(a[i]<0)
{
a[i+1]--;
a[i]+=10;
}
}
a[0]++;
while(a[a[0]]==0&&a[0]>1)
{
a[0]--;
}
for(i=a[0];i>=1;i--)
cout<<a[i];
cout<<endl;
}
else
{
cout<<"-";
for(i=1;i<=b[0];i++)
{
b[i]-=a[i];
if(b[i]<0)
{
b[i]+=10;
b[i+1]--;
}
}
b[0]++;
while(b[b[0]]==0&&b[0]>1)
b[0]--;
for(i=b[0];i>=1;i--)
cout<<b[i];
cout<<endl;
}
}
1. Judge the size , When the length is the same if(s1[i]>s2[i]) return 0; else return 1; Less consideration is given to equal situations ; Finally, if all are the same return 0;
2. After cutting , Long array length ++;
3. This method is only applicable to Add two positive numbers , If the minuend is negative , The output ’-‘ Into two numbers added , if , Minus is negative , Then add two numbers .( The first two can be merged , When outputting, we will discuss whether to output ’-‘). All negative numbers , Convert to the next number - The previous number ( All positive numbers , And all negative numbers can be merged )
边栏推荐
- A three-dimensional button
- What is the agile proportion of PMP Exam? Dispel doubts
- On-off and on-off of quality system construction
- 2022/7/2 question summary
- 服务熔断 Hystrix
- Bubble sort summary
- Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
- TF-A中的工具介绍
- BUUCTF MISC
- Kali 2018 full image download
猜你喜欢

Binary search basis

Fragment addition failed error lookup
![[turn to] MySQL operation practice (III): table connection](/img/70/20bf9b379ce58761bae9955982a158.png)
[turn to] MySQL operation practice (III): table connection

Pointnet++的改进

Do a small pressure test with JMeter tool

小程序直播+电商,想做新零售电商就用它吧!

On-off and on-off of quality system construction

object serialization
![[turn to] MySQL operation practice (I): Keywords & functions](/img/b1/8b843014f365b786e310718f669043.png)
[turn to] MySQL operation practice (I): Keywords & functions

National teacher qualification examination in the first half of 2022
随机推荐
[turn]: Apache Felix framework configuration properties
Collapse of adjacent vertical outer margins
对象的序列化
Heap sort summary
cocos2dx_ Lua card flip
Download and use of font icons
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
SDEI初探-透过事务看本质
Haut OJ 1350: choice sends candy
Yolov5 adds attention mechanism
A complete attack chain
Generate filled text and pictures
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
[leetcode] integer inversion [7]
YOLOv5添加注意力机制
Solon Logging 插件的添加器级别控制和日志器的级别控制
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Count sort
Embedded database development programming (VI) -- C API
Introduction to tools in TF-A