当前位置:网站首页>高精度计算
高精度计算
2022-06-22 14:26:00 【Stephen_Curry___】
高精度计算
(大数加法,大数减法,大数乘法,大数除法)
高精度加法
//高精度加法
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string add(string a,string b)
{
string c;
int t=0;//定义进位
int i=a.size()-1,j=b.size()-1;
for(;i>=0||j>=0||t!=0;i--,j--)
{
if(i>=0)t+=a[i]-'0';
if(j>=0)t+=b[i]-'0';
c+=t%10+'0';
t/=10;
}
return c;
}
int main()
{
string a,b,c;
cin>>a>>b;
c=add(a,b);
for(int i=c.size()-1;i>=0;i--)cout<<c[i];
return 0;
}
高精度减法
//高精度减法
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool cmp(string a,string b)//判断stringa和b的大小
{
if(a.size()!=b.size())return a.size()>b.size();
for(int i=a.size()-1;i>=0;i--)
if(a[i]!=b[i])return a[i]>b[i];
return true;
}
string sub(string a,string b)//高精度减法代码
{
string c;
int t=0;
int i=a.size()-1,j=b.size()-1;
while(i>=0||j>=0)
{
t=a[i--]-t-'0';
if(j>=0)t-=b[j--]-'0';
c+=((t+10)%10+'0');//这里是便捷处理t>=0和t<0两种情况
if(t<0)t=1;
else t=0;
}
return c;
}
int main()
{
string a,b,c;
cin>>a>>b;
int k=1;
if(cmp(a,b))//分两种情况:a>=b和a<b,下面是前者情况
{
c=sub(a,b);
for(int i=c.size()-1;i>=0;i--)
{
if(c[c.size()-k]=='0'&&i!=0)
//这里是为了消去前导0,防止出现123-120输出003的情况
{
k++;
continue;
}
cout<<c[i];
}
}
else
{
cout<<'-';
c=sub(b,a);
for(int i=c.size()-1;i>=0;i--)
{
if(c[c.size()-k]=='0'&&i!=0)
//这里也是消去前导0,防止出现120-123输出-003的情况
{
k++;
continue;
}
cout<<c[i];
}
}
}
高精度乘法(大数A*低精度b)
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string mul(string a,int b)
{
if(a=="0"||b==0)return "0";
string c;
int i=a.size()-1,t=0;
while(i>=0||t)
{
if(i>=0)t+=(a[i--]-'0')*b;
c+=t%10;
t/=10;
}
return c;
}
int main()
{
string a,c;
int b;
cin>>a>>b;
c=mul(a,b);
for(int i=c.size()-1;i>=0;i--)printf("%d",c[i]);
}
大数除法(大数A/低精度b)
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
//A/b c是商 r是余数
string div(string a,int b,int &r)
{
string c;
r=0;
for(int i=0;i<a.size();i++)
{
r=r*10+a[i]-'0';
c+=r/b+'0';
r%=b;
}
return c;
}
int main()
{
string a,c;
int b,r;
cin>>a>>b;
c=div(a,b,r);
int k=0;
for(int i=0;i<c.size();i++)
{
if(c[k]=='0'&&i!=c.size()-1)//除去输出前导0的情况
{
k++;
continue;
}
cout<<c[i];
};
cout<<endl<<r;
return 0;
}
如果内容中存在问题还请各位大佬不吝赐教orn。
边栏推荐
- RealNetworks vs. Microsoft: the battle in the early streaming media industry
- TDengine 连接器上线 Google Data Studio 应用商店
- UE4 obtains local files through blueprints
- Go all out to implement the flood control and disaster relief measures in detail and in place, and resolutely protect the safety of people's lives and property
- Charles 乱码问题解决
- 山东泰安“6·21”燃气爆炸事故后续:全面排查整治餐饮场所燃气安全隐患
- PowerPoint tutorial, how to add watermarks in PowerPoint?
- Database connection pool: stress testing
- Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
- Please, don't be brainwashed. This is the living reality of 90% of Chinese people
猜你喜欢

How to use the concat() function of MySQL

晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦

Database connection pool: stress testing

mysql的concat()函数如何用

Tree structured binary tree

Using virtual serial port to debug serial port in keil MDK

Verilog使用inout信号的方法

PHP built-in protocols (supported and encapsulated protocols)

百行代码实现基于Redis的可靠延迟队列

大会倒计时 | 亚马逊云科技创新大会邀您一起构建AI新引擎 !
随机推荐
What are the five characteristics of network security? What are the five attributes?
还可以这样搞nlp(分类)
ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
数据库连接池:代码目录
New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
极致效率,云原生数据库TDSQL-C安身立命的根本
U++ iterative Sorting Query learning notes
Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times
网站存在的价值是什么?为什么要搭建独立站
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!
模板特例化 template<>
Is it difficult for flush to open an account? Is it safe to open an account online?
Reading of double pointer instrument panel (II) - Identification of dial position
社区文章|MOSN 构建 Subset 优化思路分享
Verilog使用inout信号的方法
FreeRTOS task priority and interrupt priority
历时100天、小鱼搭建了个机器人交流社区!!现公开邀请版主中!
What happened to those who didn't go to college
向量3(静态成员)