Exponentiation hdu 1063

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 Rn 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 

Source

East Central North America 1988

  思路:

先將小數化成整數並計算小數有多少位,然後用10000000進制計算結果,將小數點加入結果中,清除小數點後沒有用的0,如果小數點後沒有如何數,清除小數點。

 代碼:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int i,j,r,n,k,aa,flag;
long long x[10004];
char a[12],ans1[10004],ans2[10004];
void init()
{
    k=0,aa=0,flag=0;
    memset(x,-1,sizeof(x));
    memset(ans1,0,sizeof(ans1));
    memset(ans2,0,sizeof(ans2));
}
int main()
{
    while(cin>>a>>n)
    {
        init();                           //初始化
        for(i=0; i<strlen(a); i++)        //將小數化成整數並計算小數有幾位
        {
            if(flag)
                k++;
            if(a[i]=='.')
            {
                flag=1;
                continue;
            }
            aa=aa*10+a[i]-'0';
        }
        if(aa==0)                         //如果這個數爲0,可以直接輸出0
        {
            cout<<"0"<<endl;
            continue;
        }
        x[0]=aa,r=0;
        for(i=1; i<n; i++)                //10000000進制進行計算
        {
            for(j=0; x[j]!=-1; j++)
            {
                long long mid=x[j]*aa+r;
                x[j]=mid%10000000;
                r=mid/10000000;
            }
            while(r!=0)
            {
                x[j]=r%10000000;
                r/=10000000;
            }
        }
        int t=0;
        for(i=0;; i++)                     //將值賦給字符串ans1
        {
            if(x[i+1]!=-1)
            {
                int p=7;
                while(p--)
                {
                    ans1[t++]=x[i]%10+'0';
                    x[i]/=10;
                }
            }
            else
                break;
        }
        while(x[i])
        {
            ans1[t++]=x[i]%10+'0';
            x[i]/=10;
        }
        int l=max(t,n*k);
        for(i=0; i<l; i++)                 //添加小數點
        {
            if(ans1[i]=='\0')
                ans1[i]='0';
            if(i<n*k)
                ans2[i]=ans1[i];
            else if(i==n*k)
            {
                flag=0;
                ans2[i]='.';
                ans2[i+1]=ans1[i];
            }
            else
                ans2[i+1]=ans1[i];
        }
        if(n*k!=0&&flag)
            ans2[strlen(ans2)]='.';
        strrev(ans2);
        while(ans2[strlen(ans2)-1]=='0')   //清除小數點後沒有用的0
            ans2[strlen(ans2)-1]='\0';
        if(ans2[strlen(ans2)-1]=='.')      //如果小數點後沒有數了,刪除小數點
            ans2[strlen(ans2)-1]='\0';
        cout<<ans2<<endl; 
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章