Ray's playground

 

POJ 1001

1001
  1 #include <iostream>
  2 #include <cmath>
  3 using namespace std;
  4 
  5 //Why the length of array should be 125?
  6 //Think about 99.999 * 25 or 9.9999 * 25
  7 //The max length will be less than 125
  8 int tempResult[125], result[125];
  9 int decimalFraction[5];
 10 
 11 //Init the global variables
 12 void Init()
 13 {
 14     int i=0;
 15     for(i=0; i<5; i++)
 16     {
 17         decimalFraction[i] = 0;
 18     }
 19 
 20     for(i=0; i<125; i++)
 21     {
 22         tempResult[i] = 0;
 23         result[i] = 0;
 24     }
 25 }
 26 
 27 int SetInput(int* exp, int* digit)
 28 {
 29     int counter = 0;
 30     bool hasDecimalPoint = false;
 31     char base[6];
 32 
 33     int i,j;
 34     while(cin >> base[0])
 35     {
 36         //Read the decimals
 37         for(i=1; i<6; i++)
 38         {
 39             cin >> base[i];
 40         }
 41         //Read the base number
 42         cin >> *exp;
 43 
 44         for(i=0; i<6; i++)
 45         {
 46             if(base[i] == '.')
 47             {
 48                 hasDecimalPoint = true;
 49                 break;
 50             }
 51         }
 52 
 53         i=0;
 54         j=4;
 55         if(hasDecimalPoint)
 56         {
 57             while(i<6)
 58             {
 59                 if(base[i] == '.')
 60                 {
 61                     counter = 5-i;
 62                     i++;
 63                     continue;
 64                 }
 65 
 66                 decimalFraction[j--= base[i++- 48;
 67             }
 68             *digit = counter;
 69         }
 70         else
 71         {
 72             while(i<5)
 73             {
 74                 decimalFraction[j--= base[++i] - 48;
 75             }
 76             *digit = 0;
 77         }
 78         return 0;
 79     }
 80 
 81     return 1;
 82 }
 83 
 84 void Multiply()
 85 {
 86     int i,j;
 87     for(j=0; j<125; j++)
 88     {
 89         result[j]= 0;
 90     }
 91 
 92     for(i=0; i<5; i++)
 93     {
 94         for(j=0; j<125; j++)
 95         {
 96             result[i+j] += tempResult[j] * decimalFraction[i];
 97         }
 98     }
 99 }
100 
101 void SumUp()
102 {
103     int i;
104     int left=0, right=0;
105     for(i=0; i<124; i++)
106     {
107         right = result[i] % 10;
108         left = (result[i]-right) / 10;
109         result[i] = right;
110         result[i+1+= left;
111     }
112 }
113 
114 void Pow(int b)
115 {
116     int j,k;
117     if(b == 0)
118     {
119         tempResult[0= 1;
120         return;
121     }
122 
123     if(b == 1)
124     {
125         for(j=0; j<5; j++)
126         {
127             tempResult[j] = decimalFraction[j];
128         }
129     }
130 
131     for(k=0; k<b-1; k++)
132     {
133         Multiply();
134         SumUp();
135         for(j=0; j<125; j++)
136         {
137             tempResult[j] = result[j];
138         }
139     }
140 }
141 
142 void Output(int digit)
143 {
144     int j= 124, k=0;
145     if(digit == 0)
146     {
147         while(!tempResult[j])
148         {
149             j--;
150         }
151 
152         while(j >=0 )
153         {
154             cout << tempResult[j--];
155         }
156 
157         return;
158     }
159 
160     while(!tempResult[k] && k < digit)
161     {
162         k++;
163     }
164 
165     while(!tempResult[j] && j >= digit)
166     {
167         j--;
168     }
169 
170     if(j != digit)
171     {
172         while(j >= digit)
173         {
174             cout << tempResult[j--];
175         }
176     }
177     else
178     {
179         cout << tempResult[j--];
180     }
181 
182     if(j != k-1)
183     {
184         cout << ".";
185     }
186 
187     while(j >=k)
188     {
189         cout << tempResult[j--];
190     }
191     cout << endl;
192 }
193 
194 int main()
195 {
196     int exp = 0, digit = 0;
197     int IsEnd = false;
198     while(1)
199     {
200         Init();
201         IsEnd = SetInput(&exp, &digit);
202 
203         if(IsEnd)
204         {
205             break;
206         }
207         
208         tempResult[0= decimalFraction[0+ decimalFraction[1* 10 + decimalFraction[2* 100 + decimalFraction[3* 1000 + decimalFraction[4* 10000;
209         if(!tempResult[0])
210         {
211             cout << "0" << endl;
212         } 
213 
214         Pow(exp);
215         Output(digit * exp);
216     }
217 
218     return 0;
219 }

 

posted on 2010-05-18 22:35  Ray Z  阅读(293)  评论(0编辑  收藏  举报

导航