Poj 1001 / OpenJudge 2951 Exponentiation

1.链接地址:

http://poj.org/problem?id=1001

http://bailian.openjudge.cn/practice/2951

2.题目:

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 127573   Accepted: 31149

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 */
{
...
}

Source

3.思路:

高精度浮点数乘法,利用大整数乘法的基础计算

(1)判断是否为小数,不为小数则转为小数

(2)获取小数点位置,计算出相乘后的小数点的位置。并去除原小数的小数点

(3)利用大整数乘法求结果

(4)补全小数点,并补零

 

tip:注意一下例子

0.0000 1 结果为 0

1.0000 0 结果为 1

0.0000 0 结果为 1

并注意小于1的浮点数要去除前导的0

4.代码:

  1 #include <string>

  2 #include <vector>

  3 #include <algorithm>

  4 #include <iostream>

  5 #include <cstdio>

  6 

  7 using namespace std;

  8 

  9 string mul(string str1,string str2)

 10 {

 11     vector<int> v_res(str1.size()+str2.size(),0);

 12     string::size_type i,j;

 13     vector<int>::size_type k,p;

 14 

 15     reverse(str1.begin(),str1.end());

 16     reverse(str2.begin(),str2.end());

 17     for(i = 0; i != str1.size(); ++i)

 18     {

 19         for(j = 0; j != str2.size(); ++j)

 20         {

 21             v_res[i+j] += (str1[i]-'0') * (str2[j] - '0');

 22         }

 23     }

 24     for(k = 0; k != v_res.size() - 1; ++k)

 25     {

 26         v_res[k+1] += v_res[k] / 10;

 27         v_res[k] = v_res[k] % 10;

 28     }

 29 

 30     for(p = v_res.size() - 1; p != -1; --p)

 31     {

 32         if(v_res[p] != 0) break;

 33     }

 34     if(p == -1) p = 0;

 35 

 36     string s_res(p+1,'0');

 37     for(k = p; k != -1; --k) s_res[p-k] = char(v_res[k] + '0');

 38     

 39 

 40     return s_res;

 41 

 42 }

 43 

 44 string real_mul(string str1,string str2)

 45 {

 46     string::size_type idx_str1_point = str1.find(".");

 47     if(idx_str1_point == string::npos)

 48     {

 49         str1 += ".0";

 50         idx_str1_point = str1.find(".");

 51     }

 52     str1.erase(idx_str1_point,1);

 53 

 54     string::size_type idx_str2_point = str2.find(".");

 55     if(idx_str2_point == string::npos)

 56     {

 57         str2 += ".0";

 58         idx_str2_point = str2.find(".");

 59     }

 60     str2.erase(idx_str2_point,1);

 61 

 62     string::size_type dec_res_len = (str1.size() - idx_str1_point) + (str2.size() - idx_str2_point);

 63 

 64     string res = mul(str1,str2);

 65 

 66     if(res.size() < dec_res_len + 1)

 67     {

 68         res = string(dec_res_len + 1 - res.size(),'0') + res;

 69     }

 70 

 71     res.insert(res.size() - dec_res_len,".");

 72 

 73     string::size_type idx_res_tail = res.find_last_not_of("0");

 74     res = res.substr(0,idx_res_tail+1);

 75 

 76     if(res[res.size() - 1] == '.') res.erase(res.size()-1);

 77 

 78     return res;

 79 }

 80 

 81 int main()

 82 {

 83     string r;

 84     int n;

 85     while(cin>>r>>n)

 86     {

 87         string res = "1";

 88         while(n--)

 89         {

 90             res = real_mul(res,r);

 91         }

 92         string::size_type idx_res_pre = res.find_first_not_of("0");

 93         if(idx_res_pre != string::npos) res.erase(0,idx_res_pre);

 94 

 95 

 96         cout<<res<<endl;

 97     }

 98 

 99 

100     return 0;

101 }

 

你可能感兴趣的:(open)