首页 > 其他 > 详细

高精度汇总

时间:2019-12-05 23:36:04      阅读:131      评论:0      收藏:0      [点我收藏+]

1 高精度加法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char num1[505],num2[505];
int numa1[505],numa2[505],numans[505];
int len1,len2,lenm,lenn;
int main( ){
    scanf("%s%s",num1+1,num2+1);
    len1=strlen(num1+1); 
    len2=strlen(num2+1);
    memset(numans,0,sizeof(numans));
    for(int i=1,j=len1;i<=len1;i++,j--) numa1[i]=num1[j]-'0';
    for(int i=1,j=len2;i<=len2;i++,j--) numa2[i]=num2[j]-'0';
    lenm=max(len1,len2);
    for(int i=1;i<=lenm+1;i++){
        numans[i]+=numa1[i]+numa2[i];
        if(numans[i]>=10){
            numans[i]-=10;
            numans[i+1]++;
        }
    }
    lenn=lenm+1;
    while(numans[lenn]==0){
        lenn--; 
        if(lenn==-1){
        cout<<"0"; 
        }
}
        for(int i=lenn;i>=1;i--){
            printf("%d",numans[i]);
        }
    return 0;
}

2 高精度减法

有符号判断

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
char num1[50005],num2[50005];
int numa1[50005],numa2[50005],ans[50005];
int len1,len2,lenn,lenm;
int main( ){
    scanf("%s%s",num1+1,num2+1);
    memset(ans,0,sizeof(ans));
    len1=strlen(num1+1);
    len2=strlen(num2+1);
    lenm=max(len1,len2);
    bool big=0;//big=1 :1比2大  big=0 :2比1大 
    if(len1!=len2) big=(len1>len2);
    else{
        for(int i=1;i<=len1;i++){
            if(num1[i]>num2[i]){
                big=1;
                break;
            }
            if(num1[i]<num2[i]){
                big=0;
                break;
            }
        }
    } 
    if(big==0){
        swap(len1,len2);
        swap(num1,num2);
    }
    for(int i=1,j=len1;i<=len1;i++,j--) numa1[i]=num1[j]-'0';
    for(int i=1,j=len2;i<=len2;i++,j--) numa2[i]=num2[j]-'0';
    for(int i=1;i<=lenm;i++){
        ans[i]=numa1[i]-numa2[i];
    }
    for(int i=1;i<=lenm;i++){
        if(ans[i]<0){
            ans[i]+=10;
            ans[i+1]--;
        }
        else continue;
    }
    lenn=lenm;
    while(ans[lenn]==0&&lenn>=0){
        lenn--;
        if(lenn==-1) {
            printf("0");
            return 0;
        }
    }
    if(big==0) printf("-");
    for(int i=lenn;i>=1;i--){
        printf("%d",ans[i]);
    }
    return 0;
} 

3 高精度除以低精度

A 本人写法

//本人写法 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char num1[205];
int ans[205],numa1[205],numa2[205]; 
int len1,len2,lenm;
int ys;//余数 
int ss;//商 
int f=0;
long long x;
int main( ){
    scanf("%s",num1+1);
    len1=strlen(num1+1);
    scanf("%lld",&x);
    if(x==0) {
        printf("0");
        return 0;
    }
    for(int i=1;i<=len1;i++)
    {
        numa1[i]=num1[i]-'0';
     } 
    for(int i=1;i<=len1;i++){
        ys=numa1[i]%x;
        ss=numa1[i]/x;
        numa1[i]=ss;
        numa1[i+1]+=ys*10;
    }
    for(int i=1;i<=len1;i++){
        if(f==0&&numa1[i]==0) printf("0");
        if(f==0&&numa1[i]>0) f=1;
        if(f==1){
            printf("%d",numa1[i]);
        }
        
    }
    return 0;
} 

B 洛谷题解其一

#include<cstdio>
#include <cstring>
using namespace std;
int a[10000];
int y; 
int l=0; 
int yushu; 
int shang;
int f=0; 
int main()
{
    char c=getchar();
    while (c>='0' && c<='9')
    {
        l++;
        a[l]=(c-'0');
        c=getchar();    
    }
    //scanf("%s",a);
    //l=strlen(a);
    scanf("%d",&y); 
    for (int i=1;i<=l;i++) 
    {
        yushu=a[i]%y;//第i位的余数 
        shang=a[i]/y;//第i位的商 
        a[i]=shang;
        a[i+1]+=yushu*10;//把余数弄到下一位
    }
    for (int i=1;i<=l;i++)
    {
        if (f==0 && a[i]>0) f=1;
        if (f==1) printf("%d",a[i]);
    }
    return 0; 
}

4 高精度乘法

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char num1[2005],num2[2005];
int numa1[2005],numa2[2005],ans[4005];
int len1,len2,lenn,lenm;
int lenn1,lenn2;
int main( ){
    scanf("%s%s",num1+1,num2+1);
    len1=strlen(num1+1);
    len2=strlen(num2+1);
    lenn1=len1;
    lenn2=len2;
    memset(numa1,0,sizeof(numa1));
    memset(numa2,0,sizeof(numa2));
    memset(ans,0,sizeof(ans));
    for(int i=1,j=len1;i<=len1;i++,j--) numa1[i]=num1[j]-'0';
    for(int i=1,j=len2;i<=len2;i++,j--) numa2[i]=num2[j]-'0';
    //for(int i=lenn1;i>=1;i--){
    //  while(numa1[i]==0) len1--;
    //}
    //for(int i=lenn2;i>=1;i--){
    //  while(numa2[i]==0) len2--;
    //}
    //前导0考虑 //判断0的情况
    for(int i=1;i<=len1;i++){
        for(int j=1;j<=len2;j++){
            ans[i+j-1]+=numa1[i]*numa2[j];
            if(ans[i+j-1]>=10){
                ans[i+j]+=ans[i+j-1]/10;
                ans[i+j-1]%=10; 
            }
        }
    } 
    lenn=len1+len2;
    for(int i=1;i<=lenn;i++){
        if(ans[i]>=10){
            ans[i+1]+=ans[i]/10;
            ans[i]%=10;
        }
    }
    while(ans[lenn]==0&&lenn>1){
        lenn--;
    }
    for(int i=lenn;i>=1;i--){
        printf("%d",ans[i]);
    }
    return 0;
}

5 高精度\(2^N\)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream> 
#include <iomanip>
using namespace std;
int ans[1005]={0};
void doubl2(int c[]){
    for(int i=1;i<=c[0];i++){
        c[i]=c[i]*2;
    }
    for(int i=1;i<=c[0];i++){
        if(c[i]>=10){
            c[i+1]+=ans[i]/10;//ans??
            c[i]%=10; 
        }
    }   
    if(c[c[0]+1]) c[0]++; 
}
void prta(int c[]){
    for(int i=1;i<=c[0];i++){
        printf("%d",c[c[0]+1-i]);
    }
}
int main( ){
    ans[1]=1;
    ans[0]=1;
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        doubl2(ans);
    }
    prta(ans);
    return 0;
}

6 高精度合集

By _cyl
cpp

include

include

include

using namespace std;
typedef long long ll;
const int N=2e4+5;
char s[N];
ll qwq,MOD;
struct bignum
{
int a[N],len;
bignum(){memset(a,0,sizeof(a));len=0;}
void push()
{
for(int i=0;i<=len;i++)
{
if(a[i]>=10)
{
if(i==len) len++;
a[i+1]+=a[i]/10;
a[i]%=10;
}
}
}
void read()
{
scanf("%s",s);
len=strlen(s)-1;
for(int i=0,j=len;i<=len;i++,j--) a[i]=s[j]-‘0‘;
}
void write()
{
for(int i=len;i>=0;i--) printf("%d",a[i]);
putchar(‘\n‘);
}
friend inline bool operator <(const bignum &x,const bignum &y)
{
if(x.len!=y.len) return x.len<y.len;
for(int i=x.len;i>=0;i--) if(x.a[i]!=y.a[i]) return x.a[i]<y.a[i];
return 0;
}
friend inline bignum operator +(const bignum &x,const bignum &y)
{
bignum ans;
ans.len=max(x.len,y.len);
for(int i=0;i<=ans.len;i++) ans.a[i]=x.a[i]+y.a[i];
ans.push();
return ans;
}
friend inline bignum operator -(const bignum &fi,const bignum &se)
{
bignum ans,x,y;
int op=1;
if(fi<se) op=-1,x=se,y=fi;
else
{
if(!(se<fi)) return ans;
x=fi,y=se;
}
//x.write();y.write();
ans.len=x.len;
for(int i=0;i<=ans.len;i++)
{
ans.a[i]=x.a[i]-y.a[i];
if(ans.a[i]<0) ans.a[i]+=10,x.a[i+1]--;
}
for(int i=ans.len;i>=0;i--) if(ans.a[i]==0) ans.len--;else break;
ans.a[ans.len]=op;
return ans;
}
friend inline bignum operator
(const bignum &x,const bignum &y)
{
bignum ans;
if((x.len==0 && x.a[0]==0) || (y.len==0 && y.a[0]==0))return ans;
ans.len=x.len+y.len;
for(int i=0;i<=x.len;i++)
for(int j=0;j<=y.len;j++)
ans.a[i+j]+=x.a[i]y.a[j];
ans.push();
return ans;
}
friend inline bignum operator /(const bignum &x,const ll &y)
{
ll alpha=0;
bignum ans;
for(int i=x.len;i>=0;i--)
{
alpha=alpha
10+x.a[i];
if(alpha>=y && ans.len==0) ans.len=i;
ans.a[i]=alpha/y;alpha%=y;
}
MOD=alpha;
return ans;
}
}f[2];
int main()
{
//f[0].read();//f[0].write();
//scanf("%lld",&qwq);//f[1].write();
//(f[0]/qwq).write();
//printf("%lld\n",MOD);
f[0].read();f[1].read();(f[0]-f[1]).write();
return 0;
}

高精度汇总

原文:https://www.cnblogs.com/liuziwen0224/p/11992386.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!