zoukankan      html  css  js  c++  java
  • 高精集合

    高精加和高精乘都不是逆运算,思路较简单就不进行解析(况且高精加还发过)

    高精乘:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    using namespace std;
    char a1[100001],b1[100001];
    int a[100001],b[100001],c[100001];
    int wei=0;
    int max(int x,int y){
        int ans;
        ans=x>y? x:y;
        return ans;
    }
    int main(){
        scanf("%s%s",a1,b1);
        int la=strlen(a1);
        int lb=strlen(b1);
        int m=max(la,lb);
        for(int i=la-1;i>=0;i--){
            a[la-i]=a1[i]-'0';
        }
        for(int i=lb-1;i>=0;i--){
            b[lb-i]=b1[i]-'0';
        }
        for(int i=1;i<=la;i++){
            wei=0;
            for(int j=1;j<=lb;j++){
                c[i+j-1]=a[i]*b[j]+wei+c[i+j-1];
                wei=c[i+j-1]/10;
                c[i+j-1]=c[i+j-1]%10;
            }
            c[i+lb]=wei;
        }
        int lc=la+lb;
        while(c[lc]==0&&lc>1)
            lc--;
        for(int i=lc;i>=1;i--){
            cout<<c[i];
        }
        return 0;
    }

    高精减:

    数据范围:10000位以内非零整数

    #include<bits/stdc++.h>
    using namespace std;
    char a[10005];
    char b[10005];
    int a1[10005];
    int b1[10005];
    int c[10005];
    int la,lb,m;
    bool nati;
    void ad(){
        for(int i=1;i<=m;i++){
            if(a1[i]>=b1[i]) c[i]=a1[i]-b1[i];
            else{
                a1[i+1]--;
                c[i]=a1[i]+10-b1[i]; //处理退位
            }
        }
    }
    void ad1(){                                 //b>a的特殊情况
        for(int i=1;i<=m;i++){
            if(b1[i]>=a1[i]) c[i]=b1[i]-a1[i];
            else{
                b1[i+1]--;
                c[i]=b1[i]+10-a1[i];
            }
        }
    }
    int main(){
        scanf("%s%s",a,b);
        la=strlen(a);
        lb=strlen(b);
        m=max(la,lb);
        for(int i=la-1;i>=0;i--) a1[la-i]=a[i]-48;
        for(int i=lb-1;i>=0;i--) b1[lb-i]=b[i]-48;
        for(int i=0;i<=m-1;i++) a[i]=a1[m-i]+48;
        for(int i=0;i<=m-1;i++) b[i]=b1[m-i]+48;
        if(strcmp(a,b)<0) nati=1;
        bool ok=0;
        if(!nati) ad();
        if(nati){
            cout<<"-";
            ad1();
        }
        for(int i=m;i>=1;i--){
            if(c[i]) ok=1;
            if(ok==0) continue;
            cout<<c[i];
        }
        if(ok==0) cout<<0;
        return 0;
    }

    高精除:

    只适用于高精除以低精

    #include<bits/stdc++.h>
    using namespace std;
    char a1[5005];
    int a[5005];
    int c[5005];
    int b;
    int main(){
        scanf("%s%d",a1,&b);
        int l=strlen(a1);
        for(int i=1;i<=l;i++) a[i]=a1[i-1]-'0';
        for(int i=1;i<=l;i++){
            c[i]=a[i]/b;
            a[i+1]+=(a[i]%b)*10;//有由高位向低位除,余数给低位
        }
        int i=0;
        while(!c[i]) i++;
        if(i!=l-1) for(i;i<=l;i++) cout<<c[i];//处理前导零
        else cout<<0;
        return 0;
    }

    这就是高精基本运算

  • 相关阅读:
    【数据库】不同数据库对于between and的处理 对于取查到的第一个的处理
    【调试】用chrome调试获得时间戳
    【js】js时间格式化
    【js】vue时间格式转化
    【js】ztree
    我的mybatis入门宝典
    mybatis一对多双向映射
    java为什么不支持多继承
    java的八种数据类型
    java中的异常 try catch
  • 原文地址:https://www.cnblogs.com/648-233/p/10659833.html
Copyright © 2011-2022 走看看