POJ 1131 octal Fractions (Java large number, octal decimal)

Source: Internet
Author: User

Octal Fractions
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6959 Accepted: 3825

Description

Fractions in octal (base 8) notation can is expressed exactly in decimal notation. For example, 0.75 in octal are 0.953125 (7/8 + 5/64) in decimal. All octal numbers of n digits to the right of the octal point can is expressed in no further than 3n decimal digits to the RI Ght of the decimal point.

Write a program to convert octal numerals between 0 and 1, inclusive, into equivalent decimal numerals.

Input

The input to your program would consist of octal numbers, one per line, to be converted. Each of the input number has the form 0.d1d2d3. DK, where the di is octal digits (0..7). There is no limit on K.

Output

Your output would consist of a sequence of lines of the form

0.d1d2d3 ... dk [8] = 0.d1d2d3 ... Dm [10]

The where the left side are the input (in octal), and the right hand side the decimal (base) equivalent. There must be no trailing zeros, i.e. Dm are not equal to 0.

Sample Input

0.750.00010.01234567

Sample Output

0.75 [8] = 0.953125 [10]0.0001 [8] = 0.000244140625 [10]0.01234567 [8] = 0.020408093929290771484375 [10]

Source

Southern African 2001
original title link: http://poj.org/problem?id=1131
Test Instructions: Enter a 8-binary number less than 0 and convert it to 10 -binary.
PS: Originally in the API to find the input is 8 binary method, but did not find!! Look at other people's Code on the net, indeed no! It's all analog, but much better than C + +.Conversion Mode:For example:0.75[8]---->7*8^ ( -1) + 5*8^ ( -2) =0.953125 [ten]
AC Code:
Import Java.math.bigdecimal;import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner s c = new Scanner (system.in), while (Sc.hasnext ()) {String str = sc.next (); BigDecimal ans = new BigDecimal (0); BigDecimal base = new BigDecimal (1), int length = Str.length (), for (int i = 2; i < length; i++) {base = Base.divide (New BigDecimal (8)); BigDecimal t = new BigDecimal (Str.charat (i)-' 0 '); ans = ans.add (t.multiply (Base));} System.out.println (str+ "[8] =" +ans+ "[10]");}}}


POJ 1131 octal fractions (large number of Java, octal to decimal)

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.