POJ1001 Exponentiation(高精度幂)

来源:互联网 发布:淘宝卖家发布宝贝教程 编辑:程序博客网 时间:2024/04/19 05:10

这里写图片描述
这道题的思路很简单;
假如求1.23的3次幂,首先是123的3次幂,用大数乘法模版,就这计算小数点就好了,小数部分两位,3次幂之后就有6位,在结果第六位再加上小数点就好了。

这道题就只用对这个字符串进行处理就行了。

代码很乱 都没有改,真的不想吐槽POJ
太黑人了
0的0次幂都有。

#include<iostream>using namespace std;#include<cstring>#include<cstdio>const int inf=0x3f3f3f3f;const int MAX=250;char a[MAX];char input[20];char numinput[20];char ans[MAX];char inputs[10];char res[MAX];char resans[MAX];int dig=0;//ff()这个函数是大数乘法模板void ff(){    int temp[MAX]= {0};    int temp1[MAX]= {0};    int coun=0;    for(int i=strlen(ans)-1; i>=0; i--)    {        temp[coun++]=ans[i]-'0';    }    int couns=0;    for(int i=strlen(input)-1; i>=0; i--)    {        temp1[couns++]=input[i]-'0';    }    int tempans[MAX];    fill(tempans,tempans+MAX,0);    for(int i=0; i<coun; i++)    {        for(int j=0; j<couns; j++)        {            tempans[i+j]+=temp[i]*temp1[j];//这里的+=要注意一下        }    }    for(int i=0; i<MAX; i++)    {        if(tempans[i]>=10)        {            int carry=tempans[i]/10;            tempans[i]%=10;            tempans[i+1]+=carry;        }    }    coun=0;    fill(ans,ans+MAX,0);    int k;    couns=0;    for(k=99; k>=0; k--)        ans[couns++]=tempans[k]+'0';    return ;}void f(int num){    fill(ans,ans+MAX,0);    strcpy(ans,input);    for(int i=2; i<=num; i++)    {        ff();    }    return ;}int main(){#ifdef ONLINE_JUDGE#else    freopen("in.txt","r",stdin);    freopen("outWA.txt","w",stdout);#endif    ios::sync_with_stdio(false);    while(cin>>input>>numinput)    {        int num=0,k=1;        fill(inputs,inputs+10,0);        strcpy(inputs,input);        dig=0;        fill(input,input+10,0);        int temp=0;        bool judge=false;        for(int i=0; i<strlen(inputs); i++)        {            if(judge)                dig++;            if(inputs[i]!='.')                input[temp++]=inputs[i];            else                judge=true;        }        num=0;        for(int i=strlen(numinput)-1; i>=0; i--)        {            num+=k*(numinput[i]-'0');            k*=10;        }        if(num==0)        {            bool yy=false;            for(int i=0; i<strlen(input); i++)            {                if(input[i]!='0'&&input[i]!='.')                    yy=true;            }            if(yy)                cout<<1<<endl;            else                cout<<0<<endl;            continue;        }        f(num);        dig*=num;        fill(res,res+MAX,0);        int c=0;        for(int i=strlen(ans)-1; i>=0; i--)        {            dig--;            if(dig==-1)            {                res[c++]='.';            }            res[c++]=ans[i];            if(dig==0&&i==0)//这里的一个特判,其实我心里是很不服气的  哪有(.12345)这个数啊,还要进行x次幂运算  最后只能特别处理一下  其实这个代码写的糟糕透了  就因为这些奇葩的测试组                res[c++]='.';        }        fill(resans,resans+MAX,0);        int counts=0;        bool used=false;        for(k=0; k<strlen(res); k++)        {            if(res[k]!='0')                used=true;            if(used)                resans[counts++]=res[k];        }        used=false;        bool is=false;        for(int i=strlen(resans)-1; i>=0; i--)        {            if(resans[i]!='0')                used=true;            if(used)            {                if(i==0&&resans[i]=='.')                    continue;                if(resans[i]!='.')                {                    is=true;                    cout<<resans[i];                }                else                {                    is=true;                    cout<<resans[i];                }            }        }        //这里的判断其实我是没有想到的,因为无限WA最后看了别人的特殊测试数组才知道        if(!is)            cout<<0;        cout<<endl;    }    return 0;}
0 0
原创粉丝点击