您的位置:首页 > 其它

POJ 1001 Exponentiation 模拟小数幂

2016-08-13 12:38 429 查看
模拟小数幂

小数点位 pos

非零末位 e

长度 len

只有三种情况 

  pos > len

  pos < e

  e < pos < len

#include <iostream>
#include <cstring>
using namespace std;
int a[500], b[500], c[500];
void Cal()
{
memset(c, 0, sizeof(c));
for(int i = 0; i < 200; i++)
for(int j = 0; j < 10; j++)
c[i+j] += a[i] * b[j];
for (int i = 0; i < 200; i++){
c[i+1] += c[i]/10;
c[i] %= 10;
}
memset(a, 0, sizeof(a));
for (int i = 0; i <= 200; i++)
a[i] = c[i];
}
char s[10];
int pos, n, len;
int main()
{
while(cin>>s>>n)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
int t = 0;
for (int i = 5; i >= 0; i--)
{
if(s[i]=='.') pos = i;
else b[t++] = s[i] - '0';
}
a[0] = 1;
for(int i = 1; i <= n; i++) Cal();
len = 200;
while(!c[len] && len>=0) len--;
int e = 0;//末尾
while(!c[e] && e<= len) e++;
pos = 5 - pos; pos *= n;//小数点
if(pos > len)
{
cout<<".";
for (int i = pos-1; i >= e; i--) cout<<c[i];
cout<<endl;
}
else if(pos <= e)//整数
{
for (int i = len; i >= pos; i--) cout<<c[i];
cout<<endl;
}
else//普通
{
for (int i = len; i >= pos; i--) cout<<c[i];
cout<<".";
for (int i = pos-1; i >= e; i--) cout<<c[i];
cout<<endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: