8

poj 1131 Octal Fractions(高精度小数进制转换) Java

 3 years ago
source link: https://zxs.io/article/187
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
poj 1131 Octal Fractions(高精度小数进制转换) Java

虽然题目那么长其实就是把8进制的浮点数转换成10进制,为了练习Java Biginteger 类 我这里用的是Java,也可以用数组模拟。

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		String str, ors;
		BigDecimal x, y, z;
		while (cin.hasNext()) {
			ors = cin.next();
			str = ors.substring(ors.indexOf(".")+1, ors.length());
			z = new BigDecimal(0);
			y = new BigDecimal(1);
			for (int i = 0; i < str.length(); i++) {
				x = new BigDecimal(str.charAt(i) - '0');
				y = y.multiply(new BigDecimal(8));
				x = x.divide(y, str.length()*3, RoundingMode.HALF_UP);
				z = z.add(x);
			}
			System.out.println(ors + " [8] = " + z + " [10]");
		}
		cin.close();
	}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK