高精度加减乘除

本文详细介绍了使用C++编程语言实现高精度的加法、减法、乘法和除法操作,包括处理字符串输入,以及处理进位和余数的过程。
摘要由CSDN通过智能技术生成

高精度加法

题目链接:高精度加法

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+5;
int a[N],b[N];
int c[N];
int n,m;
string s1,s2;

signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>s1>>s2;
	
	int n=s1.length();
	int m=s2.length();
	for(int i=1;i<=n;i++){
		a[n-i+1]+=s1[i-1]-'0';
	}
	for(int i=1;i<=m;i++){
		b[m-i+1]+=s2[i-1]-'0';
	}
	int jw=0;
	for(int i=1;i<=max(n,m);i++){
		c[i]=a[i]+b[i]+jw;
		jw=c[i]/10;
		c[i]%=10;
	}

	if(jw){
		c[max(n,m)+1]=jw;
	}
	int k=max(n,m)+1;
	while(c[k]==0)k--;

	for(int i=k;i>=1;i--){
		cout<<c[i];
	}

    return 0;
}

高精度减法

题目链接:高精度减法

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N =1e5+5;
int a[N],b[N],c[N];
string s1,s2;
bool flag=true;

signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>s1>>s2;
    int n=s1.length();
    int m=s2.length();
    if(n<m){
        flag=false;
    }
    else if(n==m){
        int l=0;
        while(s1[l]==s2[l]&&l<=n)l++;
        if(s1[l]<s2[l]){
            flag=false;
        }
    }
    int cnt1=0,cnt2=0;
    if(flag){
        for(int i=1;i<=n;i++){
            a[++cnt1]=s1[i-1]-'0';
        }
        for(int i=1;i<=m;i++){
            b[++cnt2]=s2[i-1]-'0';
        }
    }
    else{
        for(int i=1;i<=m;i++){
            a[++cnt1]=s2[i-1]-'0';
        }
        for(int i=1;i<=n;i++){
            b[++cnt2]=s1[i-1]-'0';
        }
    }

    reverse(a+1,a+1+cnt1);
    reverse(b+1,b+1+cnt2);

    int jw=0;
    for(int i=1;i<=max(n,m);i++){
        c[i]=a[i]-b[i]-jw;
        if(c[i]<0)jw=1;
        else jw=0;
        c[i]=(c[i]+10)%10;
    }

    int k=max(n,m)+1;
    while(c[k]==0&&k>1)k--;//注意k要大于1

    if(!flag)cout<<'-';
    for(int i=k;i>=1;i--){
        cout<<c[i];
    }

    return 0;
}

高精度乘法

题目链接:高精度乘法

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+5;
int a[N],b[N],c[N];
string s1,s2;

signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>s1>>s2;
    int n=s1.length();
    int m=s2.length();

    for(int i=1;i<=n;i++){
        a[n-i+1]=s1[i-1]-'0';
    }
    for(int i=1;i<=m;i++){
        b[m-i+1]=s2[i-1]-'0';
    }
    int jw=0;
    for(int i=1;i<=n;i++){
        jw=0;
        for(int j=1;j<=m;j++){
            c[i+j-1]+=a[i]*b[j]+jw;
            jw=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
        c[i+m]+=jw;
    }

    int k=n+m+1;
    while(c[k]==0&&k>1)k--;
    
    for(int i=k;i>=1;i--){
        cout<<c[i];
    }

    return 0;
}

高精度除法

题目链接:高精度除法

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+5;
int a[N],c[N];
string s1;
int b;

signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>s1>>b;
    int n=s1.length();
    for(int i=1;i<=n;i++){
        a[i]=s1[i-1]-'0';
    }

    int x=0;
    for(int i=1;i<=n;i++){
        c[i]=(x*10+a[i])/b;
        x=(x*10+a[i])%b;
    }

    int sb=1;
    while(c[sb]==0&&sb<n){
        sb++;
    }

    for(int i=sb;i<=n;i++){
        cout<<c[i];
    }
    cout<<"\n";
    cout<<x;

    return 0;
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值