HDU 1289(Hat’s IEEE)

题目不难,主要是了解 IEEE 754 格式。简单来说,输入一个 32-bit 的数 n,输出指数和小数,满足 2^指数 × 小数 = 原数,并且小数的绝对值在 [1.0, 2.0) 的范围内。(注意 n 必须用 float 类型存储)

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
	float n;
	int exponent; //指数
	while (cin >> n)
	{
		exponent = 0;
		while (fabs(n) >= 2.0) //小数的绝对值大于等于2.0
		{
			n /= 2.0;
			++exponent;
		}
		while (fabs(n) < 1.0) //小数的绝对值小于1.0
		{
			n *= 2.0;
			--exponent;
		}
		cout << exponent << " " << fixed << setprecision(6) << n << endl;
	}
	return 0;
}

继续加油。

发布了206 篇原创文章 · 获赞 1 · 访问量 8974

猜你喜欢

转载自blog.csdn.net/Intelligence1028/article/details/104864855
今日推荐