poj1001

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

C++ Code

Memory: 184K Time: 0MS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{
string s;
int n = 0;

while (cin >> s >> n)
{
int end = s.length();
int point = -1;
//检测小数点
for (int i = 0; i < end; i++)
if (s[i] == '.')
{
point = i;
break;
}
//去除小数点后的尾随0
if (point != -1)
while (end > point && s[end - 1] == '0')
end--;
//计算最终结果小数点的位置,point应为小数点后数字的数量
if (point != -1)
point = n * (end - point - 1);
int *nums = new int[6];
int *ans = new int[160];
int *temp = new int[160];
for (int i = 0; i < 6; i++)
nums[i] = 0;
for (int i = 0; i < 160; i++)
{
ans[i] = 0;
temp[i] = 0;
}
int tl = 6;
for (int i = end - 1, j = 0; i >= 0; i--, j++)
{
if (s[i] == '.')
{
j--;
}
else
nums[j] = s[i] - '0';
}
for (int i = 0; i < 6; i++)
ans[i] = nums[i];
//计算
int w = 0;
int r = 0;
for (int i = 1; i < n; i++)
{
tl = 6 * i;
for (int m = 0; m < tl; m++)
{
temp[m] = ans[m];
ans[m] = 0;
}
int j = 0, k = 0, watch = 0;
for (j = 0; j < 6; j++)
{
for (k = 0; k < tl; k++)
{
r = nums[j] * temp[k];
watch = w + r + ans[j + k];
ans[j + k] = watch % 10;
w = watch / 10;
}
}
while (w != 0)
{
ans[j + k - 1] = w % 10;
w = w / 10;
k++;
}
for (j = 0; j < 160; j++)
temp[j] = ans[j];
}
//将结果复制到string中
bool flag = false;
string answer;
for (int i = 159; i >= 0; i--)
{
if (ans[i] != 0)
flag = true;
if (flag == true)
answer.push_back('0' + ans[i]);
}
//输出0
if (answer.length() == 0)
{
answer = "0";
cout << answer << endl;
continue;
}
//为位数不足的小数添加足够的0
if (point > (int)answer.length())
{
answer.insert(0, point - answer.length(), '0');
}
//point不是-1和0的时候添加小数点
if (point >= 1)
answer.insert(answer.length() - point, 1, '.');

cout << answer << endl;
delete[] nums;
delete[] ans;
delete[] temp;
}
return 0;
}