您的位置:首页 > 其它

poj 1001 exponentiation

2011-09-27 11:34 399 查看
Exponentiation

Time Limit: 500MSMemory Limit: 10000K
Total Submissions: 92706Accepted: 22156
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

East Central North America 1988
虽然是水题,但是输入输出还是要小心谨慎,思考量不大,只要功夫花在小数的输出上面。

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <algorithm>

#define LEN 200

void Mult (int a[LEN], int b[LEN]) {
static int c[LEN];
memset (c, 0, sizeof(c));
for (int i=0; i<LEN; ++i) {
for (int k=0; i+k<LEN; ++k) {
c[i+k] += a[i] * b[k];
}
}
int car = 0;
for (int i=0; i<LEN; ++i) {
c[i] += car;
car = c[i] / 10;
c[i] = c[i] % 10;
}
memcpy (a, c, sizeof(c));
}

struct IsNotZero {
bool operator () (const int &x) {
return x != 0;
}
};

bool IsAllZero (int a[LEN], int i) {
return std::find_if (a, a + i, IsNotZero()) == a + i;
}

void Print (int a[LEN], int p) {
int i = LEN;
while (--i >= 0 && a[i] == 0) {}
if (i < 0) {
printf ("0\n");
return;
}
int zeros = p - i - 1;
if (zeros >= 0) { // type 1: leading zeros needed
printf (".");
while (zeros--) {
printf ("0");
}
while (i >= 0 && !IsAllZero (a, i + 1)) {
printf ("%d", a[i--]);
}
} else {          // type 2: leading zeros not needed
while (i > p - 1) {
printf ("%d", a[i--]);
}
if (i <= 0 || IsAllZero (a, i + 1)) {
printf ("\n");
return;
}
printf (".");
while (i >= 0 && !IsAllZero (a, i + 1)) {
printf ("%d", a[i--]);
}
}
printf ("\n");
}

void Pow (int a[LEN], int p) {
static int b[LEN];
memcpy (b, a, sizeof(b));
memset (a, 0, sizeof(b));
a[0] = 1;
while (p) {
if (p & 1) {
Mult (a, b);
}
Mult (b, b);
p >>= 1;
}
}

int main () {
int a[LEN];
int n;
int p;
char str[LEN];
while (scanf ("%s%d", str, &n) == 2) {
if (n == 0) {
printf ("1\n");
continue;
}
memset (a, 0, sizeof(a));
int i = -1;
while (++i < 6 && str[5 - i] != '.') {
a[i] = str[5 - i] - '0';
}
p = i;
while (++i < 6) {
a[i - 1] = str[5 - i] - '0';
}
Pow (a, n);
Print (a, p * n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: