高精度 a±b的实现

  • 1.高精度a+b的实现
    相关题目:hdoj1002

代码:

#include<cstdio> 
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
using namespace std;
char a[10005];
char b[10005];
int a0[10005];
int b0[10005];
int store[10005];
int main()
{
    int t;
    int i,j;
    scanf("%d",&t);
    getchar();
    int num=1;
    int p=t;
    while(p--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(a0,0,sizeof(a0));
        memset(b0,0,sizeof(b0));
        memset(store,0,sizeof(store));

        scanf("%s",a);
        scanf("%s",b);

        int ans=0;
        for(i=strlen(a)-1;i>=0;i--)
        {
            a0[i]=a[ans++]-'0';
        }
        ans=0;
        for(i=strlen(b)-1;i>=0;i--)
        {
            b0[i]=b[ans++]-'0';
        }


        int judge=0;
        int cnt=0;
        if(strlen(a)>strlen(b))
        {
            cnt=strlen(a)-1;
            judge=1;
        }
        else
        cnt=strlen(b)-1;

        if(judge)
        {
            for(i=0;i<strlen(a);i++)
            {
                store[i]=store[i]+(a0[i]+b0[i]);

                if(store[i]>=10)
                {
                    store[i+1]++;

                    store[i]=store[i]%10;
                }

            }
        }
        else
        {
            for(i=0;i<strlen(b);i++)
            {
                store[i]=store[i]+(a0[i]+b0[i]);

                if(store[i]>=10)
                {
                    store[i+1]++;

                    store[i]=store[i]%10;
                }

            }
        }

        printf("Case %d:\n",num++);

        for(i=strlen(a)-1;i>=0;i--)
        printf("%d",a0[i]);

        printf(" + ");
        for(i=strlen(b)-1;i>=0;i--)
        printf("%d",b0[i]);

        printf(" = ");
        for(i=cnt;i>=0;i--)
        printf("%d",store[i]);

        if(p!=0)
        printf("\n\n");
        else
        printf("\n");
    }

    return 0;
}
//0 85858585585
  • 2.高精度a-b的实现:

代码:

#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<string.h>
using namespace std;
int a[1005],b[1005];
int c[1005];
int main()
{
    int i,j;
    string s1,s2;
    while(cin>>s1>>s2)
    {
        if(s1==s2)
        {
            printf("0\n");
            continue;
        }

        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));

        int s1_leng,s2_leng;
        s1_leng=s1.length();
        s2_leng=s2.length();

        int judge=0; //处理结果是正数 
        if(s2_leng>s1_leng)judge=1; //处理结果是负数 


        /* 
         *    把输入的字符串倒置 
         *    从个位数开始处理 
         *
         */

        int cnt=1,tot=1;

        for(i=s1_leng-1;i>=0;i--)
        {
            a[cnt++]=s1[i]-'0';
        }
        cnt--;//a.length()

        for(i=s2_leng-1;i>=0;i--)
        {
            b[tot++]=s2[i]-'0';
        }
        tot--;//b.length()

        //长度相同时特判 

        if(s1_leng==s2_leng)
        {
            for(i=tot;i>=1;i--) //从最高位开始判断 
            {
                if(a[i]<b[i])
                {
                    judge=1; //a<b 
                    break;
                }
                else
                {
                    break;
                }
            }
        }

        if(judge)printf("-");

        if(!judge)//a.length()>b.length() +
        {
            for(i=1;i<=tot;i++)
            {
                if(a[i]<b[i])
                {
                    a[i]+=10;
                    a[i+1]-=1;
                }

                a[i]-=b[i];
            }

            for(i=cnt;i>=1;i--)
            {
                if(a[i]!=0)
                {
                    for(j=i;j>=1;j--)
                    {
                        printf("%d",a[j]);
                    }

                    break;
                }  
            }
            printf("\n");
        }
        else//b.length()>a.length() -
        {
            for(i=1;i<=cnt;i++)
            {
                if(b[i]<a[i])
                {
                    b[i]+=10;
                    b[i+1]-=1;
                }

                b[i]-=a[i];
            }

            for(i=tot;i>=1;i--)
            {
                if(b[i]!=0)
                {
                    for(j=i;j>=1;j--)
                    {
                        printf("%d",b[j]);
                    }

                    break;
                }    
            }
            printf("\n");
        }
    }
    return 0;
}
/*
1873817
1873819
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值