UVa-11809 - Floating-Point Numbers

参考了http://blog.csdn.net/xyqcl/article/details/40011009

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    double aa[15][35]={};
    int bb[15][35]={};
    for(int m=0;m<=9;m++)
    {
        for(int e=1;e<=30;e++)
        {
            double tc=1-pow(2,-m-1);
            long long td=pow(2,e)-1;
            double t=log(tc)/log(10)+td*log(2)/log(10);
            bb[m][e]=t;
            aa[m][e]=pow(10,t-bb[m][e]);
        }
    }
    char s[30]={};
    while(cin>>s&&s[0]-'0')
    {
        double a;
        int b;
        s[17]=' ';
        sscanf(s,"%lf %d",&a,&b);
        for(int m=0;m<=9;m++)
            for(int e=1;e<=30;e++)
                if(fabs(aa[m][e]-a)<1e-6&&b==bb[m][e])
                    cout<<m<<" "<<e<<endl;
    }
}

posted on 2015-02-26 23:07  windrises  阅读(187)  评论(0编辑  收藏  举报

导航