Title Link: Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=830&page=show_ problem&problem=2909
This is a maths problem!!! Mathematical slag means a good panic "shivering
We know 0<=m<=9,1<=e<=30, the data is relatively small, to play a table to solve.
First of all, we need to know that the floating-point number in the computer is divided into three parts, the front one represents the symbol, the latter part is the mantissa, the last part is the order code, the expression method is similar to the scientific notation, but binary.
(1) If there is a M-bit mantissa, the actual mantissa is: a=2^ ( -1) +2^ ( -2) +2^ ( -3) +...+2^ (-m-1) =1-2^ (-m-1);(comes with a mantissa).
(2) If there is an e-bit order, the actual index is 2^b=2^ (2^e-1);
In decimal form, the mantissa is C, and the exponent is D, then: A * 2^b = c * 10^d.
We can now take the two sides at the same time at the base of the logarithm of 10:
LOG10 (a) + b * LOG10 (2) = LOG10 (c) + D;
Because the scientific notation requires 1<=c<10, so 0<=log10 (c) <1, and D is an integer, the D=floor (x) is available; c = 10^ (x-d).
For any input of P * 10^q, look up the table one by one, as long as Q = d and Fabs (P-C) is less than the given error (1*10^ (-5)), it can be considered successful, the output of the corresponding M and E.
Note: the floor () function is rounded down. Be sure to note the data type when defining variables!!
The AC code is attached:
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cmath>5 using namespacestd;6 7 intMain ()8 {9 Doublea[Ten][ to];Ten Doublea,c; One Doublex; A Long Longb[Ten][ to]; - Long Longb,d; - inti,j; the for(i=0;i<Ten; i++) - for(j=1;j< to; j + +) {//Play Table -A=1-pow (2,-i-1); -B=pow (2, j)-1; +X=log10 (a) +b*log10 (2); -D=Floor (x); +C=pow (Ten, X-d); Aa[i][j]=C; atb[i][j]=D; - } - - Chars[ -]; - Doublep; - intQ; in - while(cin>>s) { to if(s=="0e0") + return 0; -s[ -]=' ';//Turn e into a space theSSCANF (s),"%lf%d", &p,&q);//read in P,q from S * for(i=0;i<Ten; i++) $ for(j=1;j< to; j + +)Panax Notoginseng if(Fabs (a[i][j]-p) <1e-5&&B[I][J]==Q)//Matching Output -cout<<i<<' '<<j<<Endl; the } + return 0; A}
UVa 11809-floating-point Numbers