首页 > ACM题库 > HDU-杭电 > HDU 1289 Hat’s IEEE-模拟-[解题报告] C++
2013
12-04

HDU 1289 Hat’s IEEE-模拟-[解题报告] C++

Hat’s IEEE

问题描述 :

Your program will be given floating point numbers (32-bit), and calculate
exponent and fraction.

IEEE 754:
SEEEEEEEEFFFFFFFFFFFFFFFFFFFFFFF

S = sign(1)
E = exponent(8)
F = fraction(23)

NOTE: If you donnot know IEEE 754,you can baidu it:)

输入:

Each line contains a numbers.

输出:

For each case, output a line containing two numbers, e (exponent) and f (fraction), by a single space. Fraction must be rounded to six digits after the decimal point.

样例输入:

6560.91
-6560.91

样例输出:

12 1.601785
12 -1.601785


NOTE: INTEL CPU

Problem – 1289

  好题。其实就是模拟IEEE754的格式,不过要注意的是,这里用的32位是float,用double就不对了。

代码如下:

#include <cstdio>
 #include <cmath>
 #include <iostream>
 
 using namespace std;
 
 int main() {
     float x;
     while (cin >> x) {
         int cnt = 0;
         while (fabs(x) >= 2.0) x /= 2.0, cnt++;
         while (fabs(x) < 1.0) x *= 2.0, cnt--;
         printf("%d %.6f\n", cnt, x);
     }
     return 0;
 }

 

——written by Lyon


  1. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

  2. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。