POJ1001 解题报告

来源:互联网 发布:js显示当前时间 编辑:程序博客网 时间:2024/06/06 01:04

POJ 1001
Exponentiation
Time Limit: 500MS Memory Limit: 10000KTotal 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 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 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566


解题报告

Accepted 140K47MS 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");
}

0 0