您的位置:首页 > 其它

UVA - 11809 Floating-Point Numbers

2015-03-18 00:53 288 查看
UVa11809 Floating-Point Numbers
时间限制:3.000秒
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=830&page=show_problem&problem=2909

小白书第三章的最后一题,理解上有些难度,参考了一篇很好的文章,还是看不太懂。以后再认真看看。

先贴下自己的代码:
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include<cstring>

using namespace std;

const double DET = 1e-4;

int main() 
{
	double M[20][40];
	long long E[20][40];

	// 打表
	for (int i = 0; i <= 9; ++i) 
	for (int j = 1; j <= 30; ++j) 
	{
		double m = 1 - pow(2, -1 - i), e = pow(2, j) - 1;
		double t = log10(m) + e * log10(2);
		E[i][j] = t;
		M[i][j] = pow(10, t - E[i][j]);
	}

	// 输入并输出结果
	char in[32];
	while (cin >> in)
	{
		if (strcmp(in, "0e0") == 0)
			break;
		double a;
		long long int b;
		*(strchr(in, 'e')) = ' ';              //返回‘e’首次出现的位置的指针。
		sscanf(in, "%lf %lld", &a, &b);
		for (int i = 0; i <= 9; i++)
		{
			for (int j = 1; j <= 30; j++)
			{
				if (fabs(M[i][j] - a)<DET && E[i][j] == b)
					printf("%d %d\n", i, j);
			}
		}
	}
	return 0;
}


在贴下找到的文章

http://blog.csdn.net/crazysillynerd/article/details/43339157
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: