PKU1001 题解

Posted on 2007-08-23 13:15 刘超 阅读(531) 评论(0)  编辑 收藏 引用

 

Exponentiation 
Time  Limit:500MS  Memory Limit:10000K
Total Submit:
22256  Accepted: 5036  

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


这道题目很繁琐,考虑的方面也很多,不过很锻炼编码和改错的能力
题目的解法就是一个高精度问题,用数组模拟乘法运算,这道题目值得收藏
以下是代码

  1 #include  < iostream >
  2 using   namespace  std;
  3
  4 const   int  MAX  =   1000 ;
  5
  6 int  length( int  num)
  7 {
  8    int  k  =   0 ;
  9    while (num / 10   !=   0 )
 10    {
 11     k ++ ;
 12     num  /=   10 ;
 13   }

 14    return  k + 1 ;
 15   
 16 }

 17
 18
 19 void  Multiply( int  result[],  int &  resultlen,  int  digit[],  int  k)
 20 {
 21    int  i;
 22    int  j;
 23    int  len;
 24    int  re[MAX];
 25    for  (i  =   0 ; i  <  MAX; i ++ )
 26     re[i]  =   0 ;
 27   
 28    for  (i  =   0 ; i  <  k; i ++ )
 29      for  (j  =   0 ; j  <  resultlen; j ++ )
 30      {
 31         re[i + j]  +=  result[j]  *  digit[i];
 32         re[i + j + 1 +=  re[i + j]  /   10 ;
 33         re[i + j]  %=   10 ;
 34     }

 35   len  =  resultlen  +  k;
 36    if  (re[len - 1 ==   0 )
 37     len -- ;
 38   resultlen  =  len;
 39    for  (i  =   0 ; i  <  len; i ++ )
 40     result[i]  =  re[i];
 41 }
 
 42
 43 int  main()
 44 {
 45    double  r;
 46    int  n;
 47    int  dot;
 48    int  i;
 49    int  j;
 50    int  k;
 51    int  m;
 52    int  digit[MAX];
 53    int  result[MAX];
 54    int  resultlen;
 55    bool  flag  =   true ;
 56   
 57    while  (cin  >>  r  >>  n)
 58    {
 59     i  =  length(( int )r);
 60      if  (i  ==   1 )
 61      {
 62         dot  =   4 ;
 63         r  =  r  *   10000 ;
 64     }

 65      else
 66      {
 67         dot  =   3 ;
 68         r  =  r  *   1000 ;
 69     }

 70      long   int  number  =  ( long   int )r;
 71     
 72     dot  =  dot  *  n;
 73      if  (number  ==   0
 74      {
 75           cout  <<   " 0 "   << endl;
 76            continue ;
 77     }

 78     
 79      for  (i  =   0 ; i  <  MAX; i ++ )
 80      {
 81         digit[i]  =   0 ;
 82         result[i]  =   0 ;
 83     }

 84     
 85     k  =  length(number);
 86     
 87     resultlen  =  k;
 88     
 89     
 90      for  (i  =   0 ; i  <  k; i ++ )
 91      {
 92         digit[i]  =  result[i]  =  number  %   10 ;
 93         number  /=   10 ;   
 94     }

 95   
 96      for  (i  =   0 ; i  <  n - 1 ; i ++ )
 97         Multiply(result, resultlen, digit, k);
 98         
 99     
100     i  =   0 ;
101      while  (i  <  dot)
102      {
103          if  (result[i]  !=   0 )
104            break ;
105         i ++ ;
106     }

107         
108      if  (i  ==  dot)
109         flag  =   false ;
110      else
111      {
112         flag  =   true ;
113         m  =  i;
114     }

115     
116     
117      if  (resultlen  >  dot)
118      {
119          for  (i  =  resultlen  -   1 ; i  >=  dot; i -- )
120           cout  <<  result[i];
121          if  (flag)
122          {
123           cout  <<   " . "  ;
124            for  (i  =  dot  -   1 ; i  >=  m; i -- )
125             cout  <<  result[i];
126         }
     
127     }
   
128      else
129      {
130          if  (flag)
131          {
132           cout  <<   " . "  ;
133            for  (i  =  dot  -   1 ; i  >=  m; i -- )
134             cout  <<  result[i];
135         }

136           
137     }

138         cout  <<  endl;     
139   }

140    return   0 ;
141 }

142

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


posts - 2, comments - 0, trackbacks - 0, articles - 0

Copyright © 刘超