HDU1265 floating point conversion

Verbatim https://www.dazhuanlan.com/2019/08/25/5d622bd96eb30/


Original title link

http://icpc.njust.edu.cn/Problem/Hdu/1265/

Description

Converts a real number s is represented by IEEE single precision floating point.

img

Input

First enter a number N (1 <= N <= 150), it indicates the number of instances; followed by N real numbers.

Output

Output IEEE floating point representation, capital letters.

Sample Input

1
2
3
2
23.85
-23.85

Sample Output

1
2
41BECCCD
C1BECCCD

Analysis

A clever way, the 4-byte floating-point 32-bit memory space to copy integer ANS,

Then in hexadecimal way to print an integer ans;

Variable ans use long or unsigned int, because there are 32, if int, 32-bit bit represents positive and negative, only 31 are available, it will not be enough.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include<cstdio>
#include<cstring>

using namespace std;

int (){
long n,ans;
float f;
cin>>n;
while(n--){
cin>>f;
memcpy(&ans,&f,4);
printf("%lXn",ans);
}
return 0;
}

Guess you like

Origin www.cnblogs.com/petewell/p/11408059.html