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

3.思路:

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

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

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

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

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

tip:注意一下例子

0.0000 1 结果为 0

1.0000 0 结果为 1

0.0000 0 结果为 1

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

4.代码:

 #include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio> using namespace std; string mul(string str1,string str2)
{
vector<int> v_res(str1.size()+str2.size(),);
string::size_type i,j;
vector<int>::size_type k,p; reverse(str1.begin(),str1.end());
reverse(str2.begin(),str2.end());
for(i = ; i != str1.size(); ++i)
{
for(j = ; j != str2.size(); ++j)
{
v_res[i+j] += (str1[i]-'') * (str2[j] - '');
}
}
for(k = ; k != v_res.size() - ; ++k)
{
v_res[k+] += v_res[k] / ;
v_res[k] = v_res[k] % ;
} for(p = v_res.size() - ; p != -; --p)
{
if(v_res[p] != ) break;
}
if(p == -) p = ; string s_res(p+,'');
for(k = p; k != -; --k) s_res[p-k] = char(v_res[k] + ''); return s_res; } string real_mul(string str1,string str2)
{
string::size_type idx_str1_point = str1.find(".");
if(idx_str1_point == string::npos)
{
str1 += ".0";
idx_str1_point = str1.find(".");
}
str1.erase(idx_str1_point,); string::size_type idx_str2_point = str2.find(".");
if(idx_str2_point == string::npos)
{
str2 += ".0";
idx_str2_point = str2.find(".");
}
str2.erase(idx_str2_point,); string::size_type dec_res_len = (str1.size() - idx_str1_point) + (str2.size() - idx_str2_point); string res = mul(str1,str2); if(res.size() < dec_res_len + )
{
res = string(dec_res_len + - res.size(),'') + res;
} res.insert(res.size() - dec_res_len,"."); string::size_type idx_res_tail = res.find_last_not_of("");
res = res.substr(,idx_res_tail+); if(res[res.size() - ] == '.') res.erase(res.size()-); return res;
} int main()
{
string r;
int n;
while(cin>>r>>n)
{
string res = "";
while(n--)
{
res = real_mul(res,r);
}
string::size_type idx_res_pre = res.find_first_not_of("");
if(idx_res_pre != string::npos) res.erase(,idx_res_pre); cout<<res<<endl;
} return ;
}
上一篇:android sdk manager 无法更新解决方法


下一篇:Mac 10.9x下安装配置phonegap3.0开发环境 (涉及android sdk配置)