POJ 1001 求高精度冪

來源:互聯網
上載者:User
#include <iostream>#include <cstdlib>using namespace std;const int maxstr = 1024;char res[maxstr], temp[maxstr];char R[6], newR[6];int n, ling, dian, i, newRp;int times, reslen, range;int carry, bit, reserve, base, top;void multiply();int main(){    freopen("1001.txt","r",stdin);    freopen("1001_.txt","w",stdout);    while(cin>>R>>n)    {        for(ling=5;'0'==R[ling];--ling);        for(dian=ling;'.'!=R[dian];--dian);        newRp=0;        range=n*(ling-dian);        ++ling;        for(i=0;i<dian;++i){            newR[newRp]=R[i];            newRp++;        }        for(i=dian+1;i<ling;++i){            newR[newRp]=R[i];            newRp++;        }        for(i=0;i<newRp;++i)            res[i]=newR[newRp-i-1];        for(i=0;i<newRp;++i)            newR[i]=res[i];        //while('0'==newR[newRp-1])        //    newRp--;        reslen=newRp;//-----------------------------------------------    for(i=0;i<reslen;++i)        res[i]-='0';    for(i=0;i<newRp;++i)        newR[i]-='0';//-----------------------------------------------        for(times=2;times<=n;++times){            multiply();        }//-----------------------------------------------    for(i=0;i<reslen;++i)        res[i]+='0';    for(i=0;i<newRp;++i)        newR[i]+='0';//-----------------------------------------------        if(range<reslen){            for(ling=reslen-1;'0'==res[ling];ling--);            if(ling<0){                cout<<"0";            }else{                for(i=ling;i>=range;--i)                    cout<<res[i];                if(range>0){                    cout<<'.';                    for(i=range-1;i>=0;--i)                        cout<<res[i];                }            }        }else{            cout<<".";            for(i=0;i<range-reslen;++i)                cout<<'0';            for(i=reslen-1;i>=0;--i)                cout<<res[i];        }        cout<<endl;    }    return 0;}void multiply()//multiply res[reslen] with newR[newRp];{    for(i=0;i<maxstr;++i)        temp[i]=0;    for(base=0;base<newRp;++base){        carry=0;        for(top=0;top<reslen;++top){            bit=newR[base]*res[top]+carry;            reserve=bit%10;            carry=(bit-reserve)/10;            temp[base+top]+=reserve;        }        temp[base+top]+=carry;    }    carry=0;    for(i=0;i<maxstr;++i){        bit=temp[i]+carry;        reserve=bit%10;        carry=(bit-reserve)/10;        temp[i]=reserve;    }    for(reslen=maxstr-1;0==temp[reslen];reslen--);    reslen++;    for(i=0;i<reslen;i++)        res[i]=temp[i];}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.