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

题目链接:戳这里

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;
}


    原文作者:大整数乘法问题
    原文地址: https://blog.csdn.net/WQN20172674/article/details/80601384
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞