当前位置:网站首页>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 )
边栏推荐
- object serialization
- Use the command character to close the keyboard command of the notebook
- [leetcode] integer inversion [7]
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- GBase数据库助力湾区数字金融发展
- [trans]: spécification osgi
- cocos_ Lua listview loads too much data
- Reverse one-way linked list of interview questions
- [轉]: OSGI規範 深入淺出
- UE fantasy engine, project structure
猜你喜欢
The present is a gift from heaven -- a film review of the journey of the soul
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Improvement of pointnet++
Generate filled text and pictures
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
[merge array] 88 merge two ordered arrays
2022年上半年国家教师资格证考试
Heap sort summary
质量体系建设之路的分分合合
YOLOv5-Shufflenetv2
随机推荐
room数据库的使用
支持多模多态 GBase 8c数据库持续创新重磅升级
PMP candidates, please check the precautions for PMP examination in July
2022 / 7 / 1 Résumé de l'étude
Quick sort summary
[to be continued] [UE4 notes] L1 create and configure items
服务熔断 Hystrix
Magnifying glass effect
Add level control and logger level control of Solon logging plug-in
[speed pointer] 142 circular linked list II
National teacher qualification examination in the first half of 2022
[turn]: Apache Felix framework configuration properties
Haut OJ 1245: large factorial of CDs --- high precision factorial
[转]MySQL操作实战(一):关键字 & 函数
软件测试 -- 0 序
The next key of win generates the timestamp file of the current day
嵌入式数据库开发编程(五)——DQL
Haut OJ 1243: simple mathematical problems
Learning notes of "hands on learning in depth"
[to be continued] [depth first search] 547 Number of provinces