poj 1131 hex conversion

// between POJ 1131 Octal Fractions arbitrary binary decimal conversion // given an octal decimal title requires you to convert it to a decimal number, the number of decimal places is // converted three times before converting octal decimal places without meaningless output end of zero (i.e., after zero). method I // is used by 10 and then rounding 8 (p is now assumed that the conversion of n-ary binary decimal, the same rounding by n :) // convert each one must [len-1] to the start of the highest decimal (i.e., one after the decimal point), each calculated product // g = a [j] from the lowest s * n + k (where k is a product of a carry), into the standard number of bits k = g / p, // product stored in the standard s [j] = g% p; integer k as the last one stored in the conversion of conversion result string. // I actually quite sick of this high-precision ,, but also lists the wording of one kind of JAVA. . It seems JAVA must learn point. . #include <iostream> #include <string> #include <cstdlib> #include <algorithm> using namespace std; char s1 [20], s2 [50], s3 [20]; int main () {int i, t, j, k, g, l, len ;; while (! scanf ( "% s", s1) = EOF) {l = strlen (s1); strcpy (s3, s1); len = 3 * (l-2) ; t = 0; s2 [0] = '0'; s2 [1] = '.'; j = 2; while (t <len) {k = 0; t ++; for (i = l-1; i> 1; i--) // from the lowest-order bit-by-10 using eight rounding {g = (s1 [i] - '0') * 10 + k; k = g / 8; s1 [i] = g% 8 + '0';} s2 [j] = k + '0'; j ++;} s2 [j] = '\ 0'; printf ( "% s [8] =",


Java

//用到的方法: //1.BigDecimal.add(BigDecimal); //2.BigDecimal.divide(BigDecimal); view plain import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in); while(cin.hasNext()) { String str=cin.next(); BigDecimal Fin=new BigDecimal(0); for(int i=2;i<str.length();i++) { BigDecimal sub=new BigDecimal((int)str.charAt(i)-48); BigDecimal mom=new BigDecimal(8); Fin=Fin.add(sub.divide(mom.pow(i-1))); } System.out.println(str+" [8] = "+Fin+" [10]"); } } } */

转自http://www.cnblogs.com/Mu-Tou/archive/2011/08/10/2133875.html


Reproduced in: https: //www.cnblogs.com/moiyer/archive/2011/11/09/2316154.html

Guess you like

Origin blog.csdn.net/weixin_33762130/article/details/94693178