HDOJ 1063 Exponentiation

 

         大数乘法,过不了只能说明测试用例不完备。贴几组测试用例:

5.1004 15 
41120989454.314570363993506408035342551967503175087477761156936917581824

000.10 20 
.00000000000000000001

12.010 20 
3898164373852177448724.9596914878392975482722144801842193624001

.10000 25 
.0000000000000000000000001

110000 20 
67274999493256000920100000000000000000000000000000000000000000000000000000000000000000000000000000000

1.0000 25 
1

1 0 
1

0 1 
0

0001.1 1 
1.1

然后,就是代码了:

#include<string>
#include<memory>
using namespace std;
#include<math.h>
#include<stdio.h>
#define DEPTH 1000000000
int exponent,value,fractionalLen;
__int64 result[25];
void multi()
{

    for(int i=1;i<exponent;i++)
    {
        int assist=0;
        for(int j=1;j<=result[0];j++)
        {
            result[j]=result[j]*value+assist;
            assist=result[j]/DEPTH;
            result[j]=result[j]%DEPTH;
        }
        if(assist>0)
        {
            result[0]++;
            result[result[0]]=assist;
        }
    }
}
int main()
{
    //freopen("Exponentiation.txt","r",stdin);
    char vtemp[7];
    while(scanf("%s%d",&vtemp,&exponent)!=EOF)
    {
        memset(result,0,sizeof(result));
        value=0;
        fractionalLen=0;
        for(int i=5,cnt=0;i>=0;i--)
        {
            if(vtemp[i]>='0'&&vtemp[i]<='9')
            {
                value=value+(vtemp[i]-'0')*pow(10.0,cnt);
                cnt++;
            }
            else
                fractionalLen=5-i;
        }
        while(value%10==0&&fractionalLen>0)
        {
            value/=10;
            fractionalLen--;
        }
        result[1]=value;
        result[0]=1;
        multi();
        fractionalLen*=exponent;
        char assist[10];
        sprintf(assist,"%d",result[result[0]]);
        string answer(assist);
        int len=(result[0]-1)*9+strlen(assist);
        for(int i=result[0]-1;i>=0;i--)
        {
            sprintf(assist,"%9d",result[i]);
            answer+=assist;
        }
        if(fractionalLen>len)
        {
            printf(".");
            for(int i=fractionalLen-len;i>0;i--)
                printf("0");
        }
        for(int i=0;i<len;i++)
        {
            if(i==len-fractionalLen)
                printf(".");
            if(answer[i]==' ')
                printf("0");
            else
                printf("%c",answer[i]);
        }
        printf("\n");
    }
    return 0;
}


 

阅读更多
文章标签: 测试 string 2010 c
个人分类: 基础算法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭