Hiho the 157th Tuesday decimal (binary) __ Simple math

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.