HDU-1063(大数乘法,N次方,高精度)

本文介绍了一种解决大数乘法及浮点数精度问题的方法,通过C++实现了一个计算R^n(R为实数,n为整数)的程序,确保了计算结果的精确度,特别适用于涉及高精度数值计算的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:戳这里

Problem Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. 

This problem requires that you write a program to compute the exact value of R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. 
 

Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
 

Output
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
 

Sample Input
 
  
95.123 120.4321 205.1234 156.7592 998.999 101.0100 12
 

Sample Output
 
  
548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201

思路:

不想说fa   题目可以转化成大数乘小数  给几个特殊样例吧

000.01 0 1
99.999 0 1 (n为0 且数字不为0 时输出1)
000.00 2 0
000000 2 0
00000. 2 0 (字符串中都为0的时候输出0)
1100 2 1210000
123 2 15129 (没有小数点时)
59.000 2 3481
00000  0      0  (都为0的时候输出0)

代码:

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

int main()
{
//    freopen("in.txt","r",stdin);
    int n,len,s1[100000],bit,j,flag,flag1,flag2;
    char s[100000],s2[100000];
    while(scanf("%s %d",s,&n)!=EOF)
    {
        getchar();
        flag1=0;
        int l=strlen(s);
        for(int i=0; i<l; i++)
            if(s[i]!='.'&&s[i]!='0')
            {
                flag1=1;
                break;   
            }                  //判断输入是否全为0
        if(n==0&&flag1!=0) printf("1\n");
        else if(flag1==0) printf("0\n");
        else
        {
            //去掉后导零
            len=strlen(s);
            flag2=0;
            //判断是否有小数点
            for(int i=(len-1); i>=0; i--)
                if(s[i]=='.')
                {
                    flag2=1;
                    break; 
                }
            if(flag2==1)
            {
                for(int i=(len-1); i>=0; i--)
                {
                    if(s[i]!='0'||s[i]=='.')break;
                    else if(s[i]=='0')s[i]='\0';
                }
            }
            len=strlen(s);
            memset(s1,0,sizeof(s1));
            memset(s2,'0',sizeof(s2));
            int ii=0;
            for(int i=0; i<len; i++)
            {
                if(s[i]=='.')
                {
                    continue;
                }
                else
                {
                    s2[ii]=s[i];
                    ii++;
                }
            }
            s2[ii]='\0';
            int x=atoi(s2);
            ii=0;
            flag=0;
            j=0;
            for(int i=(len-1); i>=0; i--)
            {
                if(s[i]=='.')
                {
                    flag=1;
                    continue;
                }
                s1[ii]=s[i]-'0';
                ii++;
                if(flag==0)j++;
            }
            if(flag2==0)
                j=0;
            j=j*n;
            bit=0;
            flag=0;
            for(int i=4; i>=0; i--)
            {
                if(flag==0&&s1[i]==0)continue;
                else
                {
                    flag=1;
                    bit++;
                }
            }
            for(int nn=0; nn<(n-1); nn++)
            {
                for(int i=0; i<bit; i++)
                    s1[i]*=x;
                for(int i=0; i<bit; i++)
                {
                    if(s1[i]>=10)
                    {
                        s1[i+1]+=(s1[i]/10);
                        s1[i]%=10;
                        if(i==(bit-1))bit++;
                    }
                }
            }
            if(j==0)
            {
                int ii=bit-1;
                for(; ii>=0; ii--)
                    printf("%d",s1[ii]);
                printf("\n");
            }
            else
            {
                if(bit>j)
                {
                    int le=bit-j;
                    int ii=bit-1;
                    for(int i=0; i<le; i++)
                    {
                        printf("%d",s1[ii]);
                        ii--;
                    }
                    printf(".");
                    for(; ii>=0; ii--)
                        printf("%d",s1[ii]);
                    printf("\n");
                }
                else if(bit==j)
                {
                    int ii=bit-1;
                    printf(".");
                    for(; ii>=0; ii--)
                        printf("%d",s1[ii]);
                    printf("\n");
                }
                else if(bit<j)
                {
                    printf(".");
                    int le=j-bit;
                    int ii=bit-1;
                    for(int i=0; i<le; i++)
                        printf("0");
                    for(; ii>=0; ii--)
                        printf("%d",s1[ii]);
                    printf("\n");
                }
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值