Exponentiation
Exponentiation |
Problems involving the computation of exact values of very large magnitudeand precision are common. For example, the computation of the national debtis a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value ofRn where R is a real number (0.0 < R < 99.999) and n is an integer suchthat .
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 exactvalue of R n. Leading zeros and insignificant trailing zeros should be suppressed in the output.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
Miguel A. Revilla
2000-02-09
这题其实就是实打实的高精度求解问题,要求给出一个数字,求它的n次方。先将该数以字符串读入,然后将该数的前导0与后导0去除,再将小数点的位置记录,同时将小数点也去除,之后再把字符串转换为整形数组,再来就是不断的高精度相乘了,最后注意小数点的位置,是否需要补0,并将其完全输出。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 200;
const int M = 10;
char ch_num[M];
int power;
int num1[N];
int num2[N];
int ans[N];
bool tmp_dot;
int len;
int t;
int ans_t;
void function() {
memset(ans,0,sizeof(ans));
for (int i = 0; i < ans_t; i++) {
for (int j = 0; j < t; j++) {
ans[i+j] += num1[i] * num2[j];
if (ans[i+j] > 9) {
ans[i+j+1] += ans[i+j] / 10;
ans[i+j] %= 10;
}
}
}
memset (num1,0,sizeof(num1));
for (int i = N-1; i >= 0; i--) {
if (ans[i] != 0) {
ans_t = i + 1;
break;
}
}
for (int i = 0; i < ans_t; i++)
num1[i] = ans[i];
}
int main() {
while (cin >> ch_num >> power) {
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
memset(ans,0,sizeof(ans));
len = strlen(ch_num);
tmp_dot = false;
t = 0;
//查找.的位置;
int after_dot = 0;
for (int i = 0; i < len; i++) {
if (ch_num[i] == '.')
tmp_dot = true;
else if (tmp_dot)
after_dot++;
}
//去除前导0;
for (int i = 0; i < len; i++) {
if (ch_num[i] == '.' || (ch_num[i] >= '1' && ch_num[i] <= '9'))
break;
else
ch_num[i] = '\0';
}
//去除后导0;
if (tmp_dot) {
for (int i = len-1; i >= 0; i--) {
if (ch_num[i] >= '1' && ch_num[i] <= '9')
break;
else if (ch_num[i] == '.') {
ch_num[i] = '\0';
len--;
break;
}
else {
ch_num[i] = '\0';
after_dot--;
len--;
}
}
}
//去除.
for (int i = 0; i < len; i++) {
if (ch_num[i] == '.') {
ch_num[i] = '\0';
break;
}
}
//由字符转换为整形;
for (int i = len-1; i >= 0; i--) {
if (ch_num[i]) {
num1[t] = ch_num[i] - '0';
num2[t] = ch_num[i] - '0';
t++;
}
}
//阶乘函数;
ans_t = t;
for (int i = 0; i < power - 1; i++)
function();
//输出;
int tmp;
tmp = power * after_dot;
if (tmp > ans_t) {
cout << '.';
for (int i = tmp; i > ans_t; i--)
cout << 0;
}
for (int i = ans_t-1; i >= 0; i--) {
if (i == tmp-1)
cout << '.';
cout << ans[i];
}
cout << endl;
}
return 0;
}