POJ1131 (Octal Fractions) [进制转换]

题目来源:http://poj.org/problem?id=1131

八进制与十进制的转换,如有小数部分,对应乘相应8的-i次方【字母O,表示八进制】
245O = 3x8^2+4x8^1+5x8^0 = 229

引用自https://blog.csdn.net/Jishu360/article/details/8112950

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String s=null;
BigDecimal r = BigDecimal.valueOf(0.125);
BigDecimal ans = BigDecimal.ZERO;
while(in.hasNext()) {
s = in.next();
r = BigDecimal.valueOf(0.125);
ans = BigDecimal.ZERO;
for(int i=2;i<s.length();i++) {
ans = ans.add(BigDecimal.valueOf(s.charAt(i) - '0').multiply(r));
r=r.multiply(BigDecimal.valueOf(0.125));
}
System.out.println(s+" [8] = "+ans+" [10]");
}
}
}