北大acm1001

题目链接:http://poj.org/problem?id=1001

Source Code

Problem: 1001   User: yuanting0505
Memory: 256K   Time: 0MS
Language: C++   Result: Accepted
    • Source Code
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

classbig_float
{
private:
    int num[200];//6^25 最多150位
    int pos;//pos为小数点位置
    int length;//长度
public:
   // big_float input_float(char *);//把输入的浮点数转化成 big_float型
    big_float input_float(char *in){
        big_float new_create;
        new_create.pos=0;
        new_create.length=0;
        memset(new_create.num,0,sizeof(new_create.num));//各位置零
        int i=0;
        int len=0;
        int length=(int)strlen(in);
        for(i=0;i<length;i++)
        {
            if(in[length-1-i]=='.')//from back
            {
                new_create.pos=i;
            }
            else
            {
                new_create.num[len]=in[length-1-i]-'0';//不是num[i]啊啊啊啊
                //减去0的ascll码,效果跟(int)(in[length-1-i])一样吧。。。
                len++;
            }
        }
        new_create.length=len;
        return new_create;
    }
    big_float a_b(big_float a,big_float b )//计算a*b
    {
        big_float c;//c用来存储计算结果
        c.pos=a.pos+b.pos;//小数点
        c.length=a.length+b.length;
        memset(c.num,0,sizeof(c.num));
        int i=0;
        int j=0;
        for(i=0;i<a.length;i++)//先乘
        {
            for(j=0;j<b.length;j++)
            {
                c.num[i+j]+=a.num[i]*b.num[j];
            }
        }
        //进位
        for(int k=0;k<c.length;k++)
        {
            if (c.num[k]>9)
            {
                c.num[k+1]+=c.num[k]/10;
                c.num[k]%=10;
                while(c.num[c.length])//进位进到了最高位
                    c.length++;
            }
        }
        return c;
    }
    void r_n(big_float r,int n)//计算r^n次
    {
        big_float t;
        if(n==0)
        {
            cout<<1;
        }
        else if(n==1)
        {
            r.print_float();
        }
        else
        {
            int i=0;
            t=t.a_b(r, r);
            for(i=0;i<n-2;i++)
            {
                t=t.a_b(t,r);
            }
            t.print_float();
        }
    }
    void print_float()//按要求打印a
    {
        int i=0;
        int j=0;
        while((this->num[j]==0)&&(j<pos)){//小数点之后的0可以去掉
            j++;
        }
        while((this->num[this->length-1-i]==0)&&(this->length-i>pos)){//小数点之前的0可以去掉
            i++;
        }
        /* 输出有问题 length和实际长度不一致的时候会出错
        if(this->length-i<=pos)//说明数为.34343 这种类型
        {
            cout<<'.';
            for(int k=i;k<this->length-j;k++)//打出i到j
            {
                cout<<this->num[this->length-k-1];
            }
        }
        else if(j>=pos)//数为432.这种类型
        {
            for(int k=i;k<this->length-j;k++)
            {
                cout<<this->num[this->length-k-1];
            }
        }
        else
        {
            int k=0;
            for(k=i;k<this->length-pos+i-j;k++)
            {
                cout<<this->num[this->length-k-1];
            }
            cout<<'.';
            for(k=this->length-pos+i-j;k<this->length-j;k++)
            {
                cout<<this->num[this->length-k-1];
            }
        }
        cout<<endl;
         */
        for(;i<this->length-j;i++)
        {
            if((this->length-i)==this->pos)
            {
                cout<<'.';
                cout<<this->num[length-1-i];
            }
            else{
                cout<<this->num[length-1-i];
            }
            
        }
        cout<<endl;
    }
};




int main(int argc, const char * argv[])
{
    char *input=new char[10];
    int n;
    while( cin>>input>>n)
    {
        big_float input_num;
        input_num=input_num.input_float(input);
        big_float result;
        result.r_n(input_num, n);
    }
}
posted @ 2013-07-30 11:07  OT元旦  阅读(161)  评论(0编辑  收藏  举报