《POJ 1001》 Exponentiation

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




**************************************************************************************************************
基本思路: 实数幂运算转换为整数幂运算,再利用大整数乘法, 利用字符串实现;
实现占用内存264K,时间0MS, 感觉用char*替换string会降低占用的内存;
整数幂运算使用的是分治的思想;
运算也可以使用int数组实现;
大整数乘法采用普通的笔算乘法的思想;
大整数乘法可以改进为O(n^log3)的复杂度;
大整数乘法还可以采用其他的思想计算: 直接计算结果的每一位应该是多少、、计算任意两位的计算结果后就相加;
最好再去搜搜java 和 C++ 优秀库提供的计算方法;

**************************************************************************************************************
**************************************************************************************************************
<code>
/* 与第一个版本比,只是使变量、函数的名称更易懂,同时,增加了些解释 */
#include <iostream>
#include <string>


using namespace std;


//不是真正的大整数加法, 真正的有区别
int intStringAdd(string &result, string &second, int pos) { //result为第一操作数,也存储加法运算的结果; pos表示result的第几位开始与second相加
int result_len = result.length();
int sec_len = second.length();
int jin = 0;
int i = 0;
for (; i < sec_len; ++i) {
int res = second[ sec_len - i - 1 ] - '0' + result[result_len - i - 1 - pos] - '0' + jin;
result[result_len - i - 1 - pos] = res % 10 + '0';
jin = res / 10;
}
result[result_len - i - 1 - pos] = jin + '0'; //second的位数 》= 第一个操作数result位数,因此,此处没有必要用while循环, 实际上, 此语句是可以省略的, 因为此处的jin为 0


return 0;
}


//大整数乘法, 利用string实现
int largeIntegerMultiply(const string &first, const string &second, string &result) {
int fir_len = first.length();
int sec_len = second.length();
string temp(fir_len + 1, '0');
int temp_len = temp.length();
int pos = 0;
for (int i = 0; i < sec_len; ++i) {
int jin = 0;
//计算乘数的某一位与被乘数first相乘的结果
int j = 0;
for (; j < first.length(); ++j) {
int res = (second[ sec_len - i - 1 ] - '0') * (first[ fir_len - j - 1 ] - '0') + jin;
temp[ temp_len - j - 1 ] = res % 10 + '0';
jin = res / 10;
  }
temp[ temp_len - j - 1 ] = jin + '0'; //最后要加上进位
intStringAdd(result, temp, pos);
//为下一个中间结果准备
++pos;
for (int r = 0; r < temp.length(); ++r) {
temp[r] = '0';
}
}


return 0;
}
//整数幂运算,分治思想
string largeIntegerPower(const string &intStringBase, int exp) {
if (1 == exp) {
return intStringBase;
} else if (0 == exp % 2) {
string first = largeIntegerPower(intStringBase, exp / 2);
string result(first.length() + first.length() + 1, '0');
largeIntegerMultiply(first, first, result);
return result;
} else {
string first = largeIntegerPower(intStringBase, (exp - 1) / 2);
string result(first.length() + first.length() + 1, '0');
largeIntegerMultiply(first, first, result);
string new_result(intStringBase.length() + result.length() + 1, '0');;
largeIntegerMultiply(result, intStringBase, new_result);
return new_result;
}


}


//实数的精确幂运算
string largeRealNumberPower(string &base, int exp) { //base会改变,有点不好
int pointPos = base.find_first_of('.');
string result;
if (pointPos < 0) {
result = largeIntegerPower(base, exp);  //转换为大整数幂运算
} else {
base.erase(pointPos, 1); //base会改变
result = largeIntegerPower(base, exp);  //转换为大整数幂运算
result.insert(result.length() - (base.length() - pointPos) * exp, 1, '.'); //增加小数点, 计算统一
}
//对result做满足显示要求的处理; &&&&&&&& 可以将此部分分别加到上面的if 和 else 中, 执行速度会更快 &&&&&&&&;
//整数位去0处理
int i = 0;
for (; i < result.length() && '0' == result[i]; ++i) {
}
if (i > 0) {
result.erase(0, i);
}
//小数位去末尾0处理
int j = result.length() - 1;
for (; j >= 0 && '0' == result[j]; --j) {
}
if (j >= 0 && '.' == result[j]) {   //别忘记j>=0, 底数为0时若不加j>=0, string result会越界;访问数组型时别忘记数组是否越界的思考
--j;
}
if (j != (result.length() - 1)) {
result.erase(j + 1);
}
//如何经过上述两步处理后 result为空, 则添加0
if (0 == result.length())
result.assign(1, '0');


return result;
}


int main() {
string base;    //底数
int exp; //指数

while (cin >> base >> exp) {       //输入
string result = largeRealNumberPower(base, exp);  //大实数幂运算函数
cout << result << endl;
}


return 0;
}
</code>
**************************************************************************************************************
**************************************************************************************************************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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. 输入说明 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. 输出说明 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. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 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 */ { ... } 来源 East Central North America 1988 北大OJ平台(代理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值