模板之高精度

昨天忘了这个事了QAQ

高精加:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char a1[500+10],b1[500+10];

int a[500+10],b[500+10],c[500+10];

int main()
{
    scanf("%s%s",a1,b1);
    int str1=strlen(a1),str2=strlen(b1);
    for(int i=1;i<=str1;i++) a[i]=a1[str1-i]-'0';
    for(int i=1;i<=str2;i++) b[i]=b1[str2-i]-'0';
    int q=1;
    while(q<=str1||q<=str2)
    {
        c[q]+=a[q];
        c[q]+=b[q];
        c[q+1]=c[q]/10;
        c[q]%=10;
        q++;
    }
    while(c[q]==0) q--;
    for(int i=q;i>=1;i--) cout<<c[i];
    return 0;
}

高精减:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char a1[500+10],b1[500+10];

int a[500+10],b[500+10],c[500+10];

int main()
{
    scanf("%s%s",a1,b1);
    int str1=strlen(a1),str2=strlen(b1);
    for(int i=1;i<=str1;i++) a[i]=a1[str1-i]-'0';
    for(int i=1;i<=str2;i++) b[i]=b1[str2-i]-'0';
    if(str2>str1)
    {
        for(int i=1;i<=str2;i++) swap(a[i],b[i]);
        putchar('-');
    }
    else
    {
        for(int i=str1;i>=1;i--)
        {
            if(a[i]>b[i]) break;
            else if(a[i]==b[i]) continue;
            else
            {
                for(int j=i;j>=1;j--) swap(a[j],b[j]);
                putchar('-');
                break;
            }
        }
    }
    int q=1;
    while(q<=str1||q<=str2)
    {
        c[q]+=10;
        c[q]+=a[q];
        c[q]-=b[q];
        c[q+1]=c[q]/10-1;
        c[q]%=10;
        q++;
    }
    while(c[q]==0&&q>1) q--;
    for(int i=q;i>=1;i--) cout<<c[i];
    return 0;
}

高精乘:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int SZ=500+10;

int a[SZ],b[SZ],c[SZ<<1];
char a1[SZ],b1[SZ];

int main()
{
    scanf("%s%s",a1,b1);
    int str1=strlen(a1);
    int str2=strlen(b1);
    for(int i=1;i<=str1;i++) a[i]=a1[str1-i]-'0';
    for(int i=1;i<=str2;i++) b[i]=b1[str2-i]-'0';
    for(int i=1;i<=str1;i++)
    {
        for(int j=1;j<=str2;j++)
        {
            c[i+j-1]+=a[i]*b[j];
        }
    }
    for(int i=1;i<=str1+str2+1;i++)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    int q=str1+str2+1;
    while(c[q]==0&&q>1) q--;
    for(int i=q;i>=1;i--) cout<<c[i];
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值