1. 程式人生 > >POJ 1001 Exponentiation

POJ 1001 Exponentiation

IT names columns class int systems sig hat min

Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 179524 Accepted: 43274

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

Hint

If you don‘t know how to determine wheather encounted the end of input:
s is a string and n is an integer
C++

while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work */
{
...
}

Source

East Central North America 1988 思路:先化成整數,然後跑高靜。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,sum,poi;
char s[601];
int num[6001],ans[6001],bns[6001];
void pre1(){
    for(int i=5;i>=0;i--)
        if(s[i]==.)    break;
        else sum++;
    sum*=n;int flag=0;num[0]=5;
    for(int i=0;i<=4;i++)
    if(s[i]==.)    flag=1,s[i]=s[i+1];
    else if(flag)    s[i]=s[i+1];    
    for(int i=4;i>=0;i--)    num[5-i]=s[i]-0;
    for(int i=5;i>=1;i--)    if(num[i]==0)num[0]--;else break;
    //for(int i=num[0];i>=1;i--)    cout<<num[i];cout<<endl;
}
void work(){
    memset(bns,0,sizeof(bns));
    for(int i=1;i<=num[0];i++){
        for(int j=1;j<=ans[0];j++){
            bns[j+i-1]+=num[i]*ans[j];
            bns[0]=j+i-1;
            if(bns[j+i-1]>=10){
                if(j+i>ans[0])    bns[0]=j+i;
                bns[j+i]+=bns[j+i-1]/10;
                bns[j+i-1]%=10;
            }
        }
    }
    
    for(int i=0;i<=bns[0];i++)    ans[i]=bns[i];
    for(int i=ans[0];i>=1;i--)    if(ans[i]==0)ans[0]--;else break;
}
void work1(){
    for(int i=ans[0];i>=poi;i--){
        if(i==sum)    cout<<".";
        cout<<ans[i];
    }
    cout<<endl;
}
void work2(){
    cout<<".";
    for(int i=ans[0];i>=poi;i--)
    cout<<ans[i];
    cout<<endl;
}
void work3(){
    cout<<".";
    for(int i=1;i<=sum-ans[0];i++)
    cout<<"0";
    for(int i=ans[0];i>=poi;i--)    cout<<ans[i];cout<<endl;
}
int main(){
    while(scanf("%s%d",s,&n)!=EOF){
        memset(ans,0,sizeof(ans));sum=0;
        memset(num,0,sizeof(num));pre1();
        for(int i=0;i<=5;i++)    ans[i]=num[i];
        for(int i=1;i<n;i++)    work();
        //for(int i=ans[0];i>=1;i--)    cout<<ans[i];cout<<endl;
        poi=1;
        for(int i=1;i<=ans[0];i++)
            if(ans[i]!=0||i>sum){ poi=i;break; }        
        if(ans[0]>sum)    work1();
        else if(ans[0]=sum) work2();
        else if(ans[0]<sum)    work3();
    }
}
/*
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12
*/

POJ 1001 Exponentiation