当前位置:网站首页>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 )
边栏推荐
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- 2022 / 7 / 1 Résumé de l'étude
- FVP和Juno平台的Memory Layout介绍
- YOLOv5添加注意力機制
- Haut OJ 1321: mode problem of choice sister
- 小程序直播+電商,想做新零售電商就用它吧!
- The present is a gift from heaven -- a film review of the journey of the soul
- [binary search] 34 Find the first and last positions of elements in a sorted array
- 远程升级怕截胡?详解FOTA安全升级
- [转]:Apache Felix Framework配置属性
猜你喜欢
Simple modal box
[turn to] MySQL operation practice (III): table connection
Heap sort summary
【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
[转]: OSGI规范 深入浅出
[to be continued] [UE4 notes] L1 create and configure items
Download and use of font icons
object serialization
Binary search basis
Learning notes of "hands on learning in depth"
随机推荐
[to be continued] [UE4 notes] L1 create and configure items
[to be continued] [depth first search] 547 Number of provinces
Solon Auth 认证框架使用演示(更简单的认证框架)
[allocation problem] 135 Distribute candy
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Haut OJ 1347: addition of choice -- high progress addition
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
Haut OJ 1316: sister choice buys candy III
嵌入式数据库开发编程(六)——C API
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
Unity ugui source code graphic
Haut OJ 1241: League activities of class XXX
Transport connection management of TCP
Stm32cubemx (8): RTC and RTC wake-up interrupt
[es practice] use the native realm security mode on es
Haut OJ 1245: large factorial of CDs --- high precision factorial
A three-dimensional button
sync.Mutex源码解读
Simple modal box
[转]: OSGI规范 深入浅出