北大poj-1001

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 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

#include<stdio.h>
#include<string.h>

char *touzero(char *tmp)
{
    if(*tmp=='0'&&*(tmp+1)!='\0')
    {
        tmp=tmp+1;
        tmp=touzero(tmp);
    }
    return tmp;
}

int weizero(char *tmp,int m)
{
    if(*tmp=='0')
    {
        m++;
        *tmp='\0';
        tmp=tmp-1;
        m=weizero(tmp,m);
    }
    return m;
}

int main()
{
    char s[7],d[150];
    int e[150];
    char *tmp;
    int i,j,k,n,tmpint;
    int m;
    int c;

    while(~scanf(" %s %d",s,&n))
    {
        c=0;
        k=m=-1;
        memset(e,0,sizeof(e));
        memset(d,0,sizeof(d));
        s[6]='\0';
        tmp=s+5;
        for(i=0;i<sizeof(s);i++)
        {
            if(s[i]=='.')
            {
                m=i;
                k=4-weizero(tmp,k)-i;
                break;
            }
        }
        if(m!=-1)
        {
            for(i=m;i<(sizeof(s)-1);i++)
            {
                s[i]=s[i+1];
            }
            s[i]='\0';
        }
        tmp=s;
        tmp=touzero(tmp);
        strcpy(s,tmp);
        i=0;
        while(s[i]!='\0')i++;
        j=i-1;
        do
        {
            i--;
            e[j-i]=(int)s[i]-48;
        }while(i>0);
        sscanf(s,"%d",&tmpint);
        for(i=0;i<n-1;i++)
        {
            for(j=0;j<149;j++)
            {
                e[j]=e[j]*tmpint;
            }
            for(j=0;j<149;j++)
            {
                e[j+1]=e[j+1]+e[j]/10;
                e[j]=e[j]%10;
            }
        }
        tmpint=0;
        m=0;
        for(i=0;i<150;i++)
        {
            if(e[i]!=0){m=1;break;}
        }
        if(n==0)
        printf("1\n");
        else if(m==0)
        printf("0\n");
        else
        {
            for(i=0;i<150;i++)//输出
            {
                if(tmpint==0&&e[149-i]==0&&150-i>k*n)
                {
                    continue;
                }
                tmpint=1;
                if(i==150-k*n)
                {
                    printf(".");
                }
                printf("%d",e[149-i]);
            }
            printf("\n");
        }
    }
    return 0;
}

  

    原文作者:Online Judge POJ
    原文地址: https://www.cnblogs.com/bixiongquan/p/3204606.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞