前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj-1131-(大数)八进制转化成十进制

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

作者头像
瑾诺学长
发布2018-09-21 16:25:42
6910
发布2018-09-21 16:25:42
举报
文章被收录于专栏:专注研发专注研发

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

代码语言:javascript
复制
0.75
0.0001
0.01234567

Sample Output

代码语言:javascript
复制
0.75 [8] = 0.953125 [10]
0.0001 [8] = 0.000244140625 [10]
0.01234567 [8] = 0.020408093929290771484375 [10]
代码语言:javascript
复制
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]");

}
}}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档