poj-1131-(大数)八进制转化成十进制

简介: Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.

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]
import java.util.*;   //包含集合框架、遗留的 collection 类、事件模型、日期和时间设施、
               //  国际化和各种实用工具类(字符串标记生成器、随机数生成器和位数组
                //日期Date类、堆栈Stack类、向量Vector类等 import java.math.*; //包含大数的类型 import java.io.*;       //包括:文件读写、标准设备输出等。Java中IO是以流为基础进行输入输出的,所有数据被串行化写入输出流,或者从输入流读入。 public class Main{ public static void main(String[] args){ Scanner cin= new Scanner(System.in); BigDecimal a=BigDecimal.valueOf(1),eight=BigDecimal.valueOf(8);; //定义大数类型并且赋初值 String str; while(cin.hasNext()) { BigDecimal an=BigDecimal.valueOf(0); BigDecimal b=a.divide(eight); //对大数的操作不能用简单的基本运算符,必须用函数操作
                        //add();subtract();multiply();divide();                         //加减乘除计算函数 str=cin.nextLine();            //大数是已字符串形式输入输出 for(int i=2;i<str.length();i++) { an=an.add(b.multiply(new BigDecimal(str.charAt(i)-48))); //str.charAt(i)————位置上的字符; b=b.divide(eight); } System.out.println(str+" [8]"+" = "+an.toString()+" [10]"); } }}

  

相关文章
|
6月前
|
算法 前端开发
前端算法-罗马数字转整数
前端算法-罗马数字转整数
|
6月前
|
算法
【Leetcode -405.数字转换为十六进制数 - 409.最长回文串】
【Leetcode -405.数字转换为十六进制数 - 409.最长回文串】
23 0
|
25天前
|
小程序 C语言
【C语言】输入一个十进制正整数,将它对应的二进制数的各位逆序,形成新的十进制数输出。题目分析及拓展应用。
【C语言】输入一个十进制正整数,将它对应的二进制数的各位逆序,形成新的十进制数输出。题目分析及拓展应用。
24 0
|
1月前
|
人工智能
PTA-求整数的位数及各位数字之和
求整数的位数及各位数字之和
26 4
|
3月前
|
C语言
[C语言][题]获取一个整数二进制序列中所有的偶数位和奇数位,分别打印出二进制序列
[C语言][题]获取一个整数二进制序列中所有的偶数位和奇数位,分别打印出二进制序列
25 0
|
4月前
|
存储
【二进制转换】十进制 转 二进制 (含相关题型)
【二进制转换】十进制 转 二进制 (含相关题型)
24 0
|
9月前
打印整数二进制的奇数位和偶数位
打印整数二进制的奇数位和偶数位
27 0
进制转换--(2-8)为什么2的3次方=8,所以三位变一位
进制转换--(2-8)为什么2的3次方=8,所以三位变一位
进制转换(二进制,八进制,十进制,十六进制)涵盖整数与小数部分,内容的图片全为手写【详细图解】
进制转换(二进制,八进制,十进制,十六进制)涵盖整数与小数部分,内容的图片全为手写【详细图解】
|
11月前
|
C语言
【C语言】输入一个整数,输出该数二进制表示中1的个数,其中负数用补码表示
输入一个整数,输出该数二进制表示中1的个数,其中负数用补码表示

热门文章

最新文章