Poj1131-Octal Fractions

Octal Fractions

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6669   Accepted: 3641

Description

Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.953125 (7/8 + 5/64) in decimal. All octal numbers of n digits to the right of the octal point can be expressed in no more than 3n decimal digits to the right 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 will consist of octal numbers, one per line, to be converted. Each input number has the form 0.d1d2d3 ... dk, where the di are octal digits (0..7). There is no limit on k.

Output

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

0.d1d2d3 ... dk [8] = 0.D1D2D3 ... Dm [10]
where the left side is the input (in octal), and the right hand side the decimal (base 10) equivalent. There must be no trailing zeros, i.e. Dm is not equal to 0.

Sample Input

0.75
0.0001
0.01234567

Sample Output

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

用java的大数,感觉挺好

import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(new BufferedInputStream(System.in));   ///用BufferedInputStream据说能快点
        BigDecimal ans, t, tmp;
        while(sc.hasNext()){
            String st = sc.nextLine();     ///字符串输入
            t = BigDecimal.valueOf(1);
            ans = BigDecimal.valueOf(0);

            int i, sta = st.indexOf(‘.‘);//找到第一次出现‘.‘  的位置
            for(i = sta+1; i < st.length(); ++i){              ///获取字符串长度只能用length()方法
                tmp = BigDecimal.valueOf(st.charAt(i) - ‘0‘);  //charAt()获取当前位置的字符
                t = t.divide(new BigDecimal("8"));           //大数只能和大数进行运算,Int只能与Int String只能和String
                tmp = tmp.multiply(t);//乘
                ans = ans.add(tmp);//加
            }
        System.out.println(st + " [8] = " + ans +  " [10]"); ///也可用System.out.printf();
        }
    }
}
时间: 2024-10-19 00:49:22

Poj1131-Octal Fractions的相关文章

POJ Octal Fractions(JAVA水过)

题目链接:CLICK HERE~ 虽然java一下模拟水过,但是我看到别人的一段神奇代码,贴出和大家共享. import java.math.*; import java.util.*; class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); BigDecimal Eight = new BigDecimal(8); while(cin.hasNext()){ String

POJ 1131 Octal Fractions (Java大数,八进制转十进制)

Octal Fractions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6959   Accepted: 3825 Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.953125 (7/8 + 5/64) in d

Octal Fractions 未完成

1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 void divi(int p[],int x,int *len) 5 { 6 int temp=0,i,j; 7 for(i=0;i<*len+3;i++) 8 { 9 temp=temp*10+p[i]; 10 p[i]=temp/x; 11 temp%=x; 12 } 13 14 for(i=*len+3;i>=0;i--)

Octal Fractions java秒 C++

Octal Fractions 题目抽象:   将八进制小数转换成十进制小树.小数的为数很大. 可以用java  中的BigDeciaml 秒掉.  time:297ms 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 import java.text.*; 5 6 public class Main 7 { 8 static int MS=3005; 9 public static void main(Stri

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

USACO 2.1 Ordered Fractions

Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N. Here is the set when N = 5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 Write a program that, given an integer N between

模拟 --- hdu 12878 : Fun With Fractions

Fun With Fractions Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 152, Accepted users: 32 Problem 12878 : No special judgement Problem description A rational number can be represented as the ratio of two integ