POJ 1001 Exponentiation 模拟小数幂

模拟小数幂

小数点位 pos

非零末位 e

长度 len

只有三种情况 

  pos > len

  pos < e

  e < pos < len

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 int a[500], b[500], c[500];
 5 void Cal()
 6 {
 7     memset(c, 0, sizeof(c)); 
 8     for(int i = 0; i < 200; i++)
 9         for(int j = 0; j < 10; j++)
10             c[i+j] += a[i] * b[j];
11     for (int i = 0; i < 200; i++){
12         c[i+1] += c[i]/10;
13         c[i] %= 10;
14     }
15     memset(a, 0, sizeof(a));    
16     for (int i = 0; i <= 200; i++)
17         a[i] = c[i];
18 }
19 char s[10];
20 int pos, n, len;
21 int main()
22 {
23     while(cin>>s>>n)
24     {
25         memset(a,0,sizeof(a));
26         memset(b,0,sizeof(b));
27         memset(c,0,sizeof(c));
28         int t = 0;
29         for (int i = 5; i >= 0; i--)
30         {
31             if(s[i]=='.') pos = i;
32             else b[t++] = s[i] - '0';
33         }
34         a[0] = 1;
35         for(int i = 1; i <= n; i++) Cal(); 
36         len = 200;
37         while(!c[len] && len>=0) len--;
38         int e = 0;//末尾
39         while(!c[e] && e<= len) e++;
40         pos = 5 - pos; pos *= n;//小数点 
41         if(pos > len)
42         {
43             cout<<".";
44             for (int i = pos-1; i >= e; i--) cout<<c[i];
45             cout<<endl;
46         }
47         else if(pos <= e)//整数 
48         {
49             for (int i = len; i >= pos; i--) cout<<c[i];
50             cout<<endl;
51         }
52         else//普通 
53         {
54             for (int i = len; i >= pos; i--) cout<<c[i];
55             cout<<".";
56             for (int i = pos-1; i >= e; i--) cout<<c[i];
57             cout<<endl;
58         }
59     }
60 }
我自倾杯,君且随意
原文地址:https://www.cnblogs.com/nicetomeetu/p/5767789.html