POJ1001 Exponentiation (解题报告)

来源:互联网 发布:java drawimage 效率 编辑:程序博客网 时间:2024/06/06 01:36

POJ1001 Exponentiation (解题报告)

       原文:

Exponentiation

Time Limit: 500MS

 

Memory Limit: 10000K

Total Submissions: 90091

 

Accepted: 21443

Description

Problems involving the computation of exact values ofvery large magnitude and precision are common. For example, the computation ofthe 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 whereR 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 Rand n. The R value will occupy columns 1 through 6, and the n value will be incolumns 8 and 9.

Output

The output will consist of one line for each line ofinput giving the exact value of R^n. Leading zeros should be suppressed in theoutput. Insignificant trailing zeros must not be printed. Don't print thedecimal point if the result is an integer.

SampleInput

95.123 12

0.4321 20

5.1234 15

6.7592  9

98.999 10

1.0100 12

SampleOutput

548815620517731830194541.899025343415715973535967221869852721

.00000005148554641076956121994511276767154838481760200726351203835429763013462401

43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201

Hint

If you don't know how to determine wheather encounted theend 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    */


{


...


}



题意:Rn 的大小,其中( 0.0 < R< 99.999,0 < n <=25 ) .

解题思路:

 

 

1、  求出小数的位数

2、  输出结果有4中可能

a)      无整数位+无小数位;

b)      无整数位+有小数位;

c)       有整数位+无小数位;

d)      有整数位+有小数位.

注意:S=”000000” 时要特别注意 !

源代码:

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#include<string.h>

#define MAX 300

int main()

{

       int i,j,k;

       intrepeat,po,point,stop,len;

       char s[6];

       intan1[MAX],an2[6],result[MAX];

       while(scanf("%s%d",s,&repeat)!=EOF)

       {

              memset(an1,0,sizeof(an1));//初始化数组an1

              memset(an2,0,sizeof(an2));//初始化数组an2

              memset(result,0,sizeof(result));//初始化数组result

              for(j=point=0,i=5;i>=0;i--)//赋值an1、an2.。。。

                     if(s[i]!='.')

                     {an1[j]=s[i]-'0';an2[j]=s[i]-'0';j++;}

                     else

                     {point=5-i;} //并记录小数的位数

              len=j;//记录an2的长度

              for(k=1;k<repeat;k++)

              {

                     memset(result,0,sizeof(result));

                     for(i=0;i<MAX&&i+j<MAX;i++)//开始乘

                            for(j=0;j<len;j++)

                                   result[i+j]+=an1[i]*an2[j];

                     for(i=0;i<MAX;i++)//解决进位问题

                     {

                            if(result[i]>=10)

                            {result[i+1]+=result[i]/10;result[i]=result[i]%10;}

                            an1[i]=result[i];

                     }

              }

              stop=point*repeat;//确定指整数的最后一位的位置

              for(i=0;an1[i]==0&&i<stop;i++);//从末尾跳过小数的后面无意义的零

              po=i;//记录位置

              for(i=MAX-10;an1[i]==0&&i>=stop;i--);//跳过结果前面无意义的零

              if(i==stop-1)//小数点前面没有整数

              {

                     if(po==stop)//小数点后面没有数字

                            printf("0");

                     else//小数点后面有数字

                     {

                            printf(".");

                            for(;i>=po;i--)

                                   printf("%d",an1[i]);

                     }

              }

              else//小数点前面有数字

              {

                     for(;i>=stop;i--)//输出小数点前面的数字

                            printf("%d",an1[i]);

                     if(i>=po)//判断小数点后面有没有数字

                     {

                            printf(".");

                            for(;i>=po;i--)

                            printf("%d",an1[i]);

                     }

              }

              printf("\n");//换行

       }

       return 0;

}

/*

 

95.123 12

0.4321 20

5.1234 15

6.7592  9

98.999 10

1.0100 12

 

 

548815620517731830194541.899025343415715973535967221869852721

.00000005148554641076956121994511276767154838481760200726351203835429763013462401

43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201

 

 */


 

原创粉丝点击