POJ1001 解题报告

POJ 1001
Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 141055 Accepted: 34473

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 R n 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.1075960194566


解题报告

Accepted 140K 47MS C++ 4022B 2015-02-06 22:23:01


#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#define BASELEN 6
#define EXPLIMIT 25
#define RESULTLEN ((BASELEN - 1)*EXPLIMIT)
#define BITDIF (RESULTLEN-BASELEN)


int ftod(char *,int *);
void PreciseMultiply(int *,int,int*);
void Overflow(int *i_register, int k);
void dtof(int*,int,char*);
void FormatPrintPreciseValue(char*);


int main(void)
{
char c_base[BASELEN] = { 0 };
int exp = 0;


while (scanf("%s %d", c_base, &exp) == 2)
{
int i_base[BASELEN] = { 0 };
int DotPosition = 0;
int i_result[RESULTLEN] = { 0 };
char c_result[RESULTLEN] = { 0 };


DotPosition=ftod(c_base,i_base);
PreciseMultiply(i_base,exp,i_result);
DotPosition = DotPosition*exp;
dtof(i_result,DotPosition,c_result);
FormatPrintPreciseValue(c_result);
}
}


int ftod(char *c_base, int *i_base)
{
int DotPos = 0;
for (int i = 0; i < BASELEN; i++)
{
if (c_base[i]=='.')
{
DotPos =i;
}
i_base[i] = c_base[i] - '0';
}
for (int j = DotPos; j >0 ; j--)
{
i_base[j] = i_base[j - 1];
}
i_base[0] = 0;
DotPos = 5 - DotPos;
return DotPos;
}


void PreciseMultiply(int *i_base, int exp, int i_result[RESULTLEN])
{
for (int i = 0; i < BASELEN; i++)
{
i_result[i+BITDIF] = i_base[i];
}


while (exp>1)
{
int i_register[RESULTLEN] = { 0 };
for (int j = 1; j <= 6;j++)
{
for (int k = 1; k<RESULTLEN; k++)
{
int tmp = i_base[BASELEN-j] * i_result[RESULTLEN-k];
i_register[RESULTLEN-k-j+1] += tmp % 10;
i_register[RESULTLEN-k-j] += tmp / 10;
Overflow(i_register, k);
}
}
for (int l = 0; l < RESULTLEN; l++)
{
i_result[l] = i_register[l];
}
exp--;
}
}


void Overflow(int *i_register,int k)
{
for (int i = k; i >0; i--)
{
int tmp = i_register[i];
i_register[i] = tmp % 10;
i_register[i - 1] += tmp / 10;
}
}


void dtof(int *i_result, int DotPosition, char *c_result)
{
DotPosition = RESULTLEN - 1 - DotPosition;
for (int i = 0; i < DotPosition; i++)
{
i_result[i] = i_result[i + 1];
}


for (int i = 0; i < RESULTLEN; i++)
{
c_result[i] = i_result[i]+'0';
}


c_result[DotPosition] = '.';
}


void FormatPrintPreciseValue(char *c_result)
{
int headzeromark = 0;
int trailingzeromark = RESULTLEN-1;


for (int i = 0; i < RESULTLEN; i++)
{
if (c_result[i]=='0'&&c_result[i+1]=='0')
{
headzeromark ++;
}
else
{
headzeromark++;
// if (c_result[i+1] == '.')
{
// headzeromark--;
}
break;
}
}


for (int j = RESULTLEN-1; j > 0; j--)
{
if (c_result[j]=='0'&&c_result[j-1]=='0')
{
trailingzeromark--;
}
else
{
if (c_result[j]=='0')
{
trailingzeromark--;
}
if (c_result[j-1]=='.')
{
trailingzeromark--;
}
break;
}
}


for (int k = headzeromark; k<=trailingzeromark ; k++)
{
printf("%c", c_result[k]);
}
printf("\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值