您的位置:首页 > 其它

UVA11809 - Floating-Point Numbers

2015-03-09 19:23 330 查看
做个结构体,M存位数 和 E存阶码

然后 匹配数据即可
#include <cstdio>
#include <cmath>
#include <cstring>

typedef long long ll;
const double min_differ=1e-5;

struct{
double M; //存储对应尾数
ll E;     //存储对应指数
}po[11][33];

void solve(double m,ll e){
for(int i=0;i<=9;i++)
for(int j=1;j<=30;j++)
if(e==po[i][j].E&&fabs(m-po[i][j].M)<min_differ){
printf("%d %d\n",i,j);
return;
}
}

int main(){
int i,j;
double m,t;
ll e;
char str[22];
for(i=0;i<=9;i++)
for(j=1;j<=30;j++){
e=(1<<j)-1;       //1 11 111 1111   ->  1*(10)
m=1-1.0/(1<<(i+1));
t=log10(m)+e*log10(2);
po[i][j].E=t/1;
po[i][j].M=pow(10,t-po[i][j].E);
}
while(scanf("%s",str),strcmp(str,"0e0")){
*(strchr(str,'e'))=' ';
sscanf(str,"%lf %lld",&m,&e);
solve(m,e);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: