高精度问题分析

//高精度阶乘

//高精度阶乘
#include<iostream>
#include<string>

using namespace std;
const int maxn =3000;
int A[maxn];
void Solution()
{
    int n;//输入
    cin>>n;
    memset(A,0,sizeof(A));
    A[0]=1;
    for(int i=2;i<=n;i++)//阶乘
    {
        int c=0;         //存储进位数
        for(int j=0;j<maxn;j++)
        {
            int s=A[j]*i+c;//按位相乘,加上进位数
            A[j]=s%10;     //每位不超10
            c=s/10;        //进位数
        }
    }
    for(int i=maxn-1;i>=0;i--)//输出
        if(A[i])
            cout<<A[i];
            cout<<endl;
}


//高精度幂运算

const int maxn = 3000;//存储高进度位数
int A[maxn];
int n;

void output(int c,int n)//输出
{
    int k=c*n;int t=0;
    while(A[t]!=0||t<k)
        t++;
    bool flag=false;
    for(int i=maxn-1;i>=0;i--)
    {   if(i==k)
    {
        flag=true;
        if(flag)
        cout<<".";
        else
        cout<<0<<".";
    }
        if(A[i]||flag)
        {
            flag=true;
            cout<<A[i];
        }
    }
}

void power(int t)
{
    int c=0;
    for(int i=0;i<maxn;i++)
    {
        int s=A[i]*t+c;
        A[i]=s%10;
        c=A[i]/10;
    }
}

void Solution()
{
    string s;//基数
    int n;//幂数
    while(cin>>s>>n)
    {
        memset(A,0,sizeof(A));
        int i=0;//用来记录小数点所在位数
        while(s[i]!='.')
        {
            i++;
        }
        int c=s.size()-i-1;//记录小数点的位置
        for(;i<s.size()-1;i++)//去掉小数点
            s[i]=s[i+1];
        int sum;//存储
        for(int j=0;j<s.size-1;j++)
            sum=sum*10+s[j]-'0';
        int m=0,thisSum=sum;
        while(sum)//基数放入数组
        {
            A[m++]=sum%10;
            sum=sum/10;
        }
        for(int i=1;i<n;i++)//幂运算
            power(thisSum);
         output(c,n);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值