poj-1001 Exponentiation

来源:互联网 发布:java set遍历 编辑:程序博客网 时间:2024/05/12 15:24

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.1075960194566517745610440100011.126825030131969720661201
好久不做oj的题,又变得菜得不行,真需要加把劲了
这道题如果用java编,就会很简单,因为有很多现成的东西可以用,BigDecimal也可以承受题目的计算量;
,用数组来承载乘数和被乘数,模拟乘法的思路,注意每次乘完之后要处理进位,都乘完之后,要进行反转,并在适宜的地方将小数点打印出来如果用c++编,就要做很多苦劳,跟高精乘差不多,无非是把底数一个个乘上去(先找到小数点的位置再删掉小数点,还要去掉尾部的无效0),用字符串来承载乘数和被乘数,所有都乘完之后再进行反转。
这是从另一位coder的博客拷过来的,怕以后找不着,就存着了。
/*  poj 1001  version:1.0  author:Knight  Email:S.Knight.Work@gmail.com  */#include<cstdio>#include<cstring>#include<cstdlib>#include<memory.h>using namespace std; char Result[200];//存R^N的结果 //大实数的乘法,乘数为FirMultiplier和SecMultiplier,结果存在Result中void HigRealMul(char* FirMultiplier, char* SecMultiplier, char* Result);//剔除实数尾部的无效0或小数点void CutInsignificantTail(char* StrR);//计算小数点在实数中的位数int CountPointIndex(char* StrR);//删除实数中的小数点,PointIndex为小数点在实数中从右向左数的第几位void DeletePoint(char* StrR, int PointIndex); int main(void){    char StrR[10];//R对应的字符串    int N;    int i;    int PointIndex = 0;//记录小数点在实数中从右向左数的第几位,如1.26在第3位,4在第0位     while(scanf("%s%d", StrR, &N) != EOF)    {        memset(Result, 0, 200);         CutInsignificantTail(StrR);         PointIndex = CountPointIndex(StrR);         DeletePoint(StrR, PointIndex);         strcpy(Result, StrR);         for (i=2; i<=N; i++)        {            HigRealMul(Result, StrR, Result);        }         int Len = strlen(Result);         if (Len -(PointIndex - 1) * N < 0)        {            printf(".");            for (i = Len - (PointIndex - 1) * N; i<0; i++)            {                printf("0");            }        }         for (i=0; i<Len; i++)        {            //输出小数点            if (i == Len -(PointIndex - 1) * N)            {                printf(".");            }            printf("%c", Result[i]);        }        printf("\n");        //printf("%s\n", Result);        //printf("%d\n", PointIndex);    }    return 0;} //大实数的乘法,乘数为FirMultiplier和SecMultiplier,结果存在Result中void HigRealMul(char* FirMultiplier, char* SecMultiplier, char* Result){     char TmpResult[200];    int i,j;    int k = -1;//控制TmpResult[]下标    int FirLen = strlen(FirMultiplier);    int SecLen = strlen(SecMultiplier);     memset(TmpResult, '0', 200); //模拟乘法运算    for (i=SecLen-1; i>=0; i--)    {        k++;         int FirMul;        int SecMul = SecMultiplier[i] - '0';        int Carry;//进位         for (j=FirLen-1; j>=0; j--)        {            FirMul = FirMultiplier[j] - '0';            TmpResult[k + FirLen - 1 - j] +=   FirMul * SecMul % 10;            Carry = FirMul * SecMul / 10 + (TmpResult[k + FirLen - 1 - j] - '0') / 10;            TmpResult[k + FirLen - 1 - j] = (TmpResult[k + FirLen - 1 - j] - '0') % 10 + '0';            TmpResult[k + FirLen - j] += Carry;        }    } //防止某一位的值超过9    for (k=0; k<200; k++)    {        TmpResult[k + 1] += (TmpResult[k] - '0') / 10;        TmpResult[k] = (TmpResult[k] - '0') % 10 + '0';    }//将设置字符串结束符    for (k=199; k>=0; k--)    {        if ('0' != TmpResult[k - 1])        {            TmpResult[k] = '\0';            break;        }    } //将临时存储的答案TmpResult倒转变成我们熟悉的方式,存到Result中    for (i=strlen(TmpResult)-1,j=0; i>=0; i--,j++)    {        Result[j] = TmpResult[i];    }    Result[j] = '\0'; } //剔除实数尾部的无效0或小数点void CutInsignificantTail(char* StrR){    int i;    int PointIndex = CountPointIndex(StrR);    int Len = strlen(StrR);     if (0 == PointIndex)    {        if ('.' == StrR[Len - 1])        {            StrR[Len - 1] = '\0';        }         return;    }     for (i=Len-1; i>Len-1-PointIndex; i--)    {        if ('0' == StrR[i] || '.' == StrR[i])        {            StrR[i] = '\0';        }        else        {            return ;        }    }} //计算小数点在实数中的位数int CountPointIndex(char* StrR){    int i;    int Index = 0;     for (i = strlen(StrR); i>=0; i--)    {         if ('.' == StrR[i])        {            break;        }        else        {            Index++;        }    }     if (-1 == i)    {        Index = 0;    }     return Index; } //删除实数中的小数点void DeletePoint(char* StrR, int PointIndex){    int i;    int Len = strlen(StrR);     for (i=strlen(StrR)-PointIndex; i<Len; i++)    {        StrR[i] = StrR[i+1];    }}

Java版本:
import java.math.BigDecimal;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);while(in.hasNext()) {BigDecimal num = in.nextBigDecimal();int n = in.nextInt();num = num.pow(n);String str = num.stripTrailingZeros().toPlainString();if(str.startsWith("0"))str = str.substring(1);System.out.println(str);}in.close();}}


0 0
原创粉丝点击