hdu1265 Floating Point Presentation

给定实数,希望用IEEE32位浮点数表示,并将32位二进制用16进制表示。

看到一个非常非常巧妙的办法,用float将实数f存下,p为unsigned char *,将p指向f,直接读出即可。


#include <cstdio>

using namespace std;


int main()

{

    float f;

    unsigned char *p = (unsigned char *)&f;

    int n;scanf("%d",&n);

    while (n --) {

        scanf("%f",&f);

        printf("%.2X%.2X%.2X%.2X\n",p[3],p[2],p[1],p[0]);

    }

}



猜你喜欢

转载自blog.csdn.net/sm_545/article/details/79681138