Description
Given a decimal decimal x, determine whether the binary representation of x is finite.
For example, the binary representation of the 0.5 is the binary representation of the 0.1,0.75 is that the 0.11,0.3 does not determine a finite binary representation.
input
The first line contains an integer T (1≤t≤10) that represents the number of groups to test the data.
The following T lines contain a decimal decimal X (0 < X < 1) per line. x must be with "0." At the beginning, the decimal part is not more than 100 digits.
Output
For each set of inputs, the binary representation of the output x or no (if X does not determine a finite binary representation).
Sample Input
3
0.5
0.75
0.3
Sample Output
0.1
0.11
NO
train of Thought
Multiply by 2, and judge whether the last 0 digits after the decimal point is 5, if not, the decimal can not be written in a finite binary form;
Otherwise, loop to the decimal point after the 0 o'clock algorithm end.
As to why the judgment is equal to 5, because when you consider ignoring the rounding, only 5 in the continuous by 2 o'clock probability equals 0 (that is, to eliminate the current bit), the other 1-9 of the number will continue to cycle (unable to eliminate the current bit), so in the process of rounding by 2 because of this affect to the final decimal point can be eliminated 。
AC Code
#-*-Coding:utf8-*-from
decimal import *
getcontext () prec =
def prbob (s):
ans = [] while
s!= 1.0:
# judge The last non 0
if STR (s). Rstrip (' 0 ') [-1]!= ' 5 ':
print ' NO ' break
# Multiply by 2
(S-int (s)) * D Ecimal (' 2.0 ')
ans.append (str (int (s)))
else:
print ' 0. ' + ' '. Join (ANS)
# input number
T = Int (raw_ Input ()) while
T > 0:
t = 1
# Enter decimal decimal
s = Decimal (Raw_input ())
Prbob (s)