poj1001(高精度)

                                                           Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 132438   Accepted: 32334

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
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 */
{
...
}

这题,最强大的是后台的测试数据,以至于让很多人没有AC,这题读了之后就会发现有几个关键点,一个是数据运算怎么处理,一个是做什么处理符合一些变态的数据,大数类的模拟手算。
首先说一下第一个,我们运算时候待着小数点计算式肯定不方便的,所以必须除去小数点,把数据当做整数计算完,然后再加上小数点,对于小数点位置的判断程序中说明。
第二个,题目中说了没意义的0不能输出,比如0.1 输出.1,比如100.10 输出100.1,我们运算可能会出现很多0,这就需要我们除去前导0,后缀零,判断数据是否大于1
第三个倒是最好解决的,数据以字符接收,存入整型数组,注意存的时候最好倒着存储,这样进位好进位一些,满十进一,输出倒着输出就OK了。
 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int output[120];   //第一位为1,方便计算
 5 int m;
 6 //num是去掉小数点后的十进制数字,比如95.23变为9523
 7 //因为我们要乘的数是固定的,所以可以都去掉小数点计算,最后如果有小数点加上去就行了
 8 void cal(char str[],int num)
 9 {
10     int i;
11     for(i=0;i<m;i++)
12     {
13         output[i]=output[i]*num;       //因为最大的数可能为99999,99999*9不会超出int范围的,所以先乘好,然后再考虑进位
14     }
15     for(i=0;i<m-1;i++)
16     {
17         if(output[i]>=10)
18         {
19             output[i+1]+=output[i]/10;
20             output[i]%=10;
21         }
22     }
23     int t = output[m-1];
24     int p=m-1;
25     if(t>=10)   //把最后一个数字比如45,存储到字符数组中
26     {
27         while(t>0)
28         {
29             output[p++]=t%10;
30             t/=10;
31         }
32     }
33     m=p;
34     return ;
35 }
36 
37 int main()
38 {
39     char str[6];
40     int n;
41     while(scanf("%s%d",str,&n)!=EOF)
42     {
43 
44         int sum = 0,Pointsum=0;
45         int length = strlen(str),i;
46         for(i =0 ; i<length;i++)
47         {
48             if(str[i]=='.')
49             {
50                 Pointsum = (length-(i+1))*n;   //记录有多少位小数
51             }
52             else
53             {
54                 sum = sum*10 + str[i]-'0';       //迭代增加求完整的十进制数
55             }
56         }
57         if(sum==0)
58         {
59             printf("0\n");
60             continue;
61         }
62         memset(output,0,sizeof(output));
63         output[0]=1;
64         m=1;
65         for(i =0 ;i<n;i++)
66             cal(str,sum);
67 
68         int temp = 0;
69         for(i =0 ; i<m;i++)  //先去掉后缀零
70         {
71             if(output[i]!=0)
72             {
73                 temp = i;         //第一位不为零的数字
74                 break;
75             }
76         }
77         if(Pointsum-temp<=0)   //没有小数点
78             for(i =m-1;i>=Pointsum;i--)    //temp也是非零的
79                 printf("%d",output[i]);
80         else      //有小数点
81         {
82             if(Pointsum>m)
83                 m=Pointsum;
84             for(i=m-1;i>=Pointsum;i--)
85                 printf("%d",output[i]);
86             printf(".");
87             for(;i>=temp;i--)
88                 printf("%d",output[i]);
89         }
90         printf("\n");
91 
92     }
93     return 0;
94 }

 

posted @ 2014-06-13 10:17  Fighting_frank  阅读(3198)  评论(0编辑  收藏  举报