poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 138526   Accepted: 33859

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

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 */
{
...
}
遥远时代的第一道题,真是怀念啊,才学C语言的我明白了高精的意义
#include<iostream>

#include <string>

using namespace std;

string  multiply(string a,string d){

    int remain=0,temp=0;

    string ad,mul="0";

    for(int i=a.length()-1;i>=0;i--){

        remain=0;//余数

        ad="";//每次的乘积

        for(int j=d.length()-1;j>=0;j--){

            temp=(a[i]&15)*(d[j]&15)+remain;

            remain=temp/10;

            temp%=10;

            ad=(char)(temp+'0')+ad;

        }

        if(remain){

            ad=(char)(remain+'0')+ad;

        }

        int x=ad.length()-1;

        int y=mul.length()-(a.length()-i);

        remain=0;

        while(x>=0&&y>=0){//完成加和

            temp=(ad[x]&15)+(mul[y]&15)+remain;

            remain=temp/10;

            temp%=10;

            mul[y]=temp+'0';

            x--;y--;

        }

        while(x>=0){//如果ad串更长

            temp=(ad[x]&15)+remain;

            remain=temp/10;

            temp%=10;

            mul=(char)(temp+'0')+mul;

            x--;

        }

        while(y>=0){//如果mul串更长

            temp=(mul[y]&15)+remain;

            remain=temp/10;

            temp%=10;

            mul[y]=temp+mul[y];

            y--;

        }

        if(remain){//如果还有余数

            mul=(char)(remain+'0')+mul;

        }

    }

    return mul;

}

int main(){

    string a,d;

    int n;

    while(cin>>a>>n){

        int pos =a.find_first_of('.');//寻找小数点位置

        a.erase(pos,1);

        int num=(a.length()-pos)*n;

        d=a;

        for(int i=1;i<n;i++){

            d=multiply(d,a);

        }

        if(pos==a.npos){//如果没有找到,a是整数,运算不会出现点,可以直接输出

            for(int i=0;i<d.length()-1;i++){

                if(d[0]!='0'){break;}

                d.erase(0,1);

            }

            cout<<d<<endl;

            continue;

        }

        d.insert(d.length()-num,".");

        for(int i=0;i<d.length();i++){//去除前导零

            if(d[0]!='0')break;

            d.erase(0,1);

        }

        for(int i=d.length()-1;i>=0;i--){//去除后置零

            if(d[i]!='0')break;

            d.erase(i,1);

        }

        if(d[d.length()-1]=='.'){//没有小数部分就不用输出.

            d.erase(d.length()-1,1);

        }

        cout<<d<<endl;

    }

    return 0;

}

  



你可能感兴趣的:(java)