当前位置:网站首页>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 )
边栏推荐
- Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
- [turn]: Apache Felix framework configuration properties
- The next key of win generates the timestamp file of the current day
- sync.Mutex源码解读
- Embedded database development programming (V) -- DQL
- Cocos2dx screen adaptation
- [轉]: OSGI規範 深入淺出
- object serialization
- [paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
- [to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
猜你喜欢
随机推荐
Solon 框架如何方便获取每个请求的响应时间?
To the distance we have been looking for -- film review of "flying house journey"
Bucket sort
[to be continued] [UE4 notes] L2 interface introduction
被舆论盯上的蔚来,何时再次“起高楼”?
Merge sort
嵌入式数据库开发编程(零)
2022上半年全国教师资格证下
[turn]: OSGi specification in simple terms
ssh免密登录设置及使用脚本进行ssh登录并执行指令
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
How can the Solon framework easily obtain the response time of each request?
Development error notes
A three-dimensional button
Haut OJ 1321: mode problem of choice sister
JVM call not used once in ten years
Service fusing hystrix
[allocation problem] 135 Distribute candy
sync.Mutex源码解读
嵌入式数据库开发编程(六)——C API