一道acm的简单题,高手门帮忙啊解决方案
一道acm的简单题,高手门帮忙啊
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
就是那么大的数,用什么来存放啊!?
------解决方案--------------------
存大数的一种方法就是可以逢n进1。
比如32位的int最大到2147483647,当你要存一个大于2147483647的数时可以表示为 n × 2147483647 + x。例如2147483648 = 1 × 2147483647 + 1,所以你用两个int,一个存n,另一个存x即可。如果两个不够用还可以三个、四个……只要逢n进一即可。
------解决方案--------------------
首先R是从-99999到99999的
n是从1-25的,你最多用25个就可以了.
------解决方案--------------------
搞个跑的特别慢的 .....
#include <stdio.h>
typedef unsigned long ul;
void slove( ul x , ul n , ul w )
{
char s_op [10*100] , *ps;
ul var [10*100], vv = 1, i , j , *p;
var [0] = 1;
for( i = 0; i < n; ++i )
{
for( j = 0; j < vv; ++j )
var[j] *= x;
for( j = 1; j < vv; ++j )
var[j] += var[j-1]/10 , var[j-1]%=10;
for( j = vv - 1; var[j] > = 10; ++j )
var[j+1] = var[j]/10 , var[j]%= 10;
vv = j+1;
}
for( *( ps = s_op + vv ) = 0 , p = var; ps > s_op; *--ps = (char)(*p++ + '0 ') )
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
就是那么大的数,用什么来存放啊!?
------解决方案--------------------
存大数的一种方法就是可以逢n进1。
比如32位的int最大到2147483647,当你要存一个大于2147483647的数时可以表示为 n × 2147483647 + x。例如2147483648 = 1 × 2147483647 + 1,所以你用两个int,一个存n,另一个存x即可。如果两个不够用还可以三个、四个……只要逢n进一即可。
------解决方案--------------------
首先R是从-99999到99999的
n是从1-25的,你最多用25个就可以了.
------解决方案--------------------
搞个跑的特别慢的 .....
#include <stdio.h>
typedef unsigned long ul;
void slove( ul x , ul n , ul w )
{
char s_op [10*100] , *ps;
ul var [10*100], vv = 1, i , j , *p;
var [0] = 1;
for( i = 0; i < n; ++i )
{
for( j = 0; j < vv; ++j )
var[j] *= x;
for( j = 1; j < vv; ++j )
var[j] += var[j-1]/10 , var[j-1]%=10;
for( j = vv - 1; var[j] > = 10; ++j )
var[j+1] = var[j]/10 , var[j]%= 10;
vv = j+1;
}
for( *( ps = s_op + vv ) = 0 , p = var; ps > s_op; *--ps = (char)(*p++ + '0 ') )