01:浮点数求高精度幂

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int a[201];
string s;int n;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define repm(i,j,k) for(int i=j;i>=k;i--)
#define mem(a) memset(a,0,sizeof(a))
int main()
{
    while(cin>>s>>n)
    {
        int dot_pos=-1;
        mem(a);
        rep(i,0,5)
        if(s[i]=='.')
        dot_pos=i;
        if(dot_pos==-1)
        {
            int ans=0;
            rep(i,0,5)
            {
                ans*=10;
                ans+=s[i]-48; 
            }
            a[200]=ans;
            repm(i,200,0)
            {
                if(a[i]>=10)
                {
                    a[i-1]+=a[i]/10;
                    a[i]%=10;
                }
            }
            rep(i,2,n)
            {
                rep(j,0,200)
                {
                    a[j]*=ans;
                }
                repm(j,200,0)
                {
                    if(a[j]>=10)
                    {
                        a[j-1]+=a[j]/10;
                        a[j]%=10;
                    }
                }
            }
            int st=0;
            while(a[st]==0)
            st++;
            rep(i,st,200)
            cout<<(char)(a[i]+48);
            cout<<endl;
            continue;
        }
        int final_pos=5;
        while(s[final_pos]=='0')
        final_pos--;
        int ans=0;
        rep(i,0,final_pos)
        {
            if(i!=dot_pos)
            {
                ans*=10;
                ans+=s[i]-48;
            }
        }
        a[200]=ans;
        repm(i,200,0)
        {
            if(a[i]>=10)
            {
                a[i-1]+=a[i]/10;
                a[i]%=10;
            }
        }
        rep(i,2,n)
        {
            rep(j,0,200)
            {
                a[j]*=ans;
            }
            repm(j,200,0)
            {
                if(a[j]>=10)
                {
                    a[j-1]+=a[j]/10;
                    a[j]%=10;
                }
            }
        }
        int st=0;
        while(a[st]==0)
        st++;
        st--;
        int len=final_pos-dot_pos;
        len*=n;
        if(200-st<=len)
        {
            cout<<".";
            rep(i,200-len+1,200)
            cout<<(char)(a[i]+48);
            cout<<endl;
        }    
        else
        {
            rep(i,st+1,200-len)
            cout<<(char)(a[i]+48);
            if(len!=0)
            cout<<".";
            rep(i,200-len+1,200)
            cout<<(char)(a[i]+48);
            cout<<endl;
        }
    }
} 

 

posted @ 2016-01-28 12:07  OZTOET  阅读(423)  评论(0编辑  收藏  举报