UVa 11809 Floating-Point Numbers

来源:互联网 发布:js escape解码 编辑:程序博客网 时间:2024/04/29 19:34
Floating-point numbers are represented differently in computers than integers. That is why a 32-bit
floating-point number can represent values in the magnitude of 1038 while a 32-bit integer can only
represent values as high as 232 .
Although there are variations in the ways floating-point numbers are stored in Computers, in this
problem we will assume that floating-point numbers are stored in the following way:
Floating-point numbers have two parts mantissa and exponent. M -bits are allotted for mantissa
and E bits are allotted for exponent. There is also one bit that denotes the sign of number (If this
bit is 0 then the number is positive and if it is 1 then the number is negative) and another bit that
denotes the sign of exponent (If this bit is 0 then exponent is positive otherwise negative). The value of
mantissa and exponent together make the value of the floating-point number. If the value of mantissa
is m then it maintains the constraints 1 ≤ m < 1. The left most digit of mantissa must always be 1 to
2
maintain the constraint 1 ≤ m < 1. So this bit is not stored as it is always 1. So the bits in mantissa
2
actually denote the digits at the right side of decimal point of a binary number (Excluding the digit
just to the right of decimal point)
In the figure above we can see a floating-point number where M = 8 and E = 6. The largest value
this floating-point number can represent is (in binary) 0.1111111112 × 21111112 . The decimal equivalent
to this number is: 0.998046875 × 263 = 920535763834529382410 . Given the maximum possible value
represented by a certain floating point type, you will have to find how many bits are allotted for
mantissa (M ) and how many bits are allotted for exponent (E) in that certain type.
Input
The input file contains around 300 line of input. Each line contains a floating-point number F that
denotes the maximum value that can be represented by a certain floating-point type. The floating point
number is expressed in decimal exponent format. So a number AeB actually denotes the value A×10B .
A line containing ‘0e0’ terminates input. The value of A will satisfy the constraint 0 < A < 10 and
will have exactly 15 digits after the decimal point.
Output
For each line of input produce one line of output. This line contains the value of M and E. You can
assume that each of the inputs (except the last one) has a possible and unique solution. You can also
assume that inputs will be such that the value of M and E will follow the constraints: 9 ≥ M ≥ 0 and
30 ≥ E ≥ 1. Also there is no need to assume that (M + E + 2) will be a multiple of 8.
Sample Input
5.699141892149156e76
9.205357638345294e18
0e0
Sample Output
5 8

8 6

#include "stdio.h"#include "string.h"#include "math.h"#include "stdlib.h"double A[10][31];long long int B[10][31];char str[100];int main(){memset(A, 0, sizeof(A));memset(B, 0, sizeof(B));for(int m = 0; m <= 9; m++){for(int e = 1; e <= 30; e++){double a = 1.0 - pow(2.0, -(m+1));long long int b = pow(2.0, e) - 1;double x = log10(a) + b * log10(2);B[m][e] = floor(x);A[m][e] = pow(10, x - B[m][e]);}}memset(str, 0, sizeof(str));while(scanf("%s", str) && strcmp(str, "0e0") != 0){int len = strlen(str);int i;for(i = 0; i < len; i++){if(str[i] == 'e')break;}char str2[100];memset(str2, 0, sizeof(str2));strncpy(str2, &str[0], i);//printf("str2: %s\n", str2);double a = atof(str2);memset(str2, 0, sizeof(str2));                strncpy(str2, &str[i+1], len-(i+1));//printf("str2: %s\n", str2);int b = atoi(str2);/*str[17]=' ';double a;int b;sscanf(str,"%lf %d",&a,&b);*/int flag = 0;for(int m = 0; m <= 9; m++){for(int e = 1; e <= 30; e++)                {if(b == B[m][e] && fabs(a-A[m][e]) < 0.00001){printf("%d %d\n", m, e);flag = 1;break;}}if(flag)break;}memset(str, 0, sizeof(str));}return 0;}


这道题一直想不起来怎么做,最终参考了http://blog.csdn.net/xyqcl/article/details/40011009

将十进制和二进制表达式同时取对数,并求floor。感觉很巧妙

0 0
原创粉丝点击