1 题目
http://poj.org/problem?id=1001
Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 188015 Accepted: 45212
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
2 代码
#include <iostream>
using namespace std;
#define RESULT_SIZE 512
void multiply(char *aChars, char * bChars, char * rChars) {
// init int a[], b[], reuslt[]
int *result = new int[RESULT_SIZE + 1]();
int an = strlen(aChars), bn = strlen(bChars);
int *a = new int[an]();
int *b = new int[bn]();
for (int i = 0; i < an; i++) a[i] = (int)(aChars[an - i - 1] - '0');
for (int i = 0; i < bn; i++) b[i] = (int)(bChars[bn - i - 1] - '0');
// compute a * b
for (int i = 0; i < an; i++) {
// use each digit of a to multiply each digit of b
for (int j = 0; j < bn; j++) {
result[i + j] += a[i] * b[j];
// carry the result
for (int k = i + j; k < RESULT_SIZE; k++) {
result[k + 1] += result[k] / 10;
result[k] %= 10;
}
}
}
int p; // the first position that is not zero
for (p = RESULT_SIZE - 1; p >= 0; p--) {
if (result[p] != 0) break;
}
for (int i = 0; i <= p; i++) {
rChars[i] = (char)result[p - i] + '0';
}
delete[] result;
delete[] a;
delete[] b;
}
int main()
{
char r[7], rNoDot[6], result[RESULT_SIZE];
int n;
memset(r, 0, 7);
memset(rNoDot, 0, 6);
memset(result, 0, RESULT_SIZE);
while (cin >> r >> n) {
int dotPos = 0;
bool afterDot = false;
for (int i = 0, j = 0; i < 7 && r[i] != NULL; i++) {
if (afterDot) {
dotPos++;
}
if (r[i] != '.') {
rNoDot[j++] = r[i];
}
else {
afterDot = true;
}
}
result[0] = '1';
for (int i = 0; i < n; i++) {
multiply(result, rNoDot, result);
}
int dotPosNow = n * dotPos;
int resultLen = strlen(result);
if (dotPosNow == 0) {
// not float dot
cout << result << endl;
}
else {
// has float dot, firstly delete tail zero
int tailZero = 0;
for (int i = 0; i < dotPosNow; i++, tailZero++) {
if (result[resultLen - 1 - i] != '0') {
break;
}
}
result[resultLen - tailZero] = NULL;
int newLen = strlen(result);
dotPosNow -= (resultLen - newLen);
resultLen = newLen;
if (dotPosNow == 0) {
cout << result << endl;
}
else if (dotPosNow <= resultLen) {
for (int i = 0; i < resultLen - dotPosNow; i++) {
cout << result[i];
}
cout << '.' << (result + resultLen - dotPosNow) << endl;
}
else {
int zeroAfterDot = (dotPosNow - resultLen);
cout << ".";
for (int i = 0; i < zeroAfterDot; i++) {
cout << '0';
}
cout << result << endl;
}
}
memset(r, 0, 7);
memset(rNoDot, 0, 6);
memset(result, 0, RESULT_SIZE);
}
return 0;
}