UVA748 Exponentiation

回老家玩了两天,回来先把没做完的题A了。一看输出就想起是poj的第二题哈(第一道A+B),当时看不太会,现在总算研究出来了。虽然是浮点运算但也能看成bign类型。细节地方稍加处理就行了。

发现个问题,这个代码过不去poj1001。有待解决!!

题目:

Exponentiation

 

  Exponentiation 

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 \le 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 Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.

 

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

 

 


Miguel A. Revilla
2000-02-09

 

解答:

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 struct bign
 5 {
 6     int len;
 7     int s[1000];
 8     int mark;
 9 };
10 bign res,a;
11 char str[100];
12 void change(char *str,bign *b)
13 {
14     int i,p=0;
15     b->len=strlen(str)-1;
16     for(i=strlen(str)-1;i>=0;i--)
17     {
18         if(str[i]=='.')
19             b->mark=b->len-i;
20         else
21         {
22             b->s[p]=str[i]-'0';
23             p++;
24         }
25     }
26 }
27 void mult(bign *a,bign *res)
28 {
29     int i,j,len,tmp[1000];
30     len=res->len+a->len;
31     memset(tmp,0,sizeof(tmp));
32     for(i=0;i<res->len;i++)
33     {
34         for(j=0;j<a->len;j++)
35         {
36             tmp[i+j]+=res->s[i]*a->s[j];
37         }
38     }
39     for(i=0;i<len-1;i++)
40     {
41         tmp[i+1]+=tmp[i]/10;
42         tmp[i]%=10;
43     }
44     for(i=0;i<len;i++)
45         res->s[i]=tmp[i];
46     res->len=len;
47     res->mark+=a->mark;
48 }
49 int main()
50 {
51     while(scanf("%s",str)!=EOF)
52     {
53         int n;
54         scanf("%d",&n);
55         int i;
56         memset(res.s,0,sizeof(res.s));
57         res.len=1;
58         res.mark=0;
59         res.s[0]=1;
60         change(str,&a);
61         for(i=0;i<n;i++)
62         {
63             mult(&a,&res);
64         }
65         int beg=res.len,end=0;
66         for(i=0;i<=res.mark-1;i++)
67         {
68             if(res.s[i]!=0||(i+1)==res.mark)
69             {
70                 break;
71             }
72         }
73         end=i;
74         for(i=res.len;i>=res.mark;i--)
75         {
76             if(res.s[i]!=0)
77             {
78                 break;
79             }
80         }
81         beg=i;
82         for(i=res.len;i>=0;i--)
83         {
84             if(i>=end&&i<=beg)
85             {
86                 printf("%d",res.s[i]);
87             }
88             if(res.mark==i)
89                 printf(".");
90         }
91         printf("\n");
92     }
93     return 0;
94 }

 

 

 

posted @ 2013-02-03 20:30  上白泽慧音  阅读(329)  评论(0编辑  收藏  举报