当前位置:网站首页>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 )
边栏推荐
猜你喜欢
随机推荐
[to be continued] [UE4 notes] L3 import resources and project migration
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Quick sort summary
Simple modal box
Solon Auth 认证框架使用演示(更简单的认证框架)
Haut OJ 1218: maximum continuous sub segment sum
cocos2dx_ Lua card flip
A preliminary study of sdei - see the essence through transactions
Use the command character to close the keyboard command of the notebook
Shell Sort
Embedded database development programming (VI) -- C API
National teacher qualification examination in the first half of 2022
[to be continued] [depth first search] 547 Number of provinces
小程序直播+电商,想做新零售电商就用它吧!
Haut OJ 1221: a tired day
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
Haut OJ 1316: sister choice buys candy III
对象的序列化
Add level control and logger level control of Solon logging plug-in
Introduction to tools in TF-A


![[to be continued] [UE4 notes] L1 create and configure items](/img/20/54ba719be2e51b7db5b7645b361e26.jpg)


![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)



