趣闻进制转换
- 輸入一個(gè)十進(jìn)數(shù),將其轉(zhuǎn)換成 N (大于0小于16)進(jìn)制數(shù)
public class text5 {/*** 輸入一個(gè)十進(jìn)數(shù),將其轉(zhuǎn)換成 N 進(jìn)制數(shù)(0<N<=16)。*/public static void main(String[] args) {char arr[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B', 'C', 'D', 'E', 'F' };Scanner scanner = new Scanner(System.in);int k = Integer.parseInt(scanner.next());int k1 = Integer.parseInt(scanner.next());String p = "";char arr1[] = new char[k1];for (int i = 0; i < k1; i++) {arr1[i] = arr[i];}while (k != 0) {int d = k % k1;p += arr[d];k = k / k1;}for (int i = p.length() - 1; i >= 0; i--) {System.out.print(p.charAt(i));}}
}
2.輸入兩個(gè)正整數(shù)X,Y,將X,Y化為二進(jìn)制數(shù),然后將這兩個(gè)二進(jìn)制數(shù)作二進(jìn)制加法運(yùn)算,再將結(jié)果化為十進(jìn)制數(shù)輸出
public class text8 {/*** 輸入兩個(gè)正整數(shù)X,Y,將X,Y化為二進(jìn)制數(shù), 然后將這兩個(gè)二進(jìn)制數(shù)作二進(jìn) 制加法運(yùn)算,再將結(jié)果化為十進(jìn)制數(shù)輸出。*/private static ArrayList<Integer> list = new ArrayList<Integer>();public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int k = Integer.parseInt(scanner.next());int k1 = Integer.parseInt(scanner.next());String str = Integer.toBinaryString(k);// 將十進(jìn)制的數(shù)字轉(zhuǎn)換為二進(jìn)制(系統(tǒng))String str1 = Integer.toBinaryString(k1);add(str, str1);String str3 = "";for (int j = list.size() - 1; j >= 0; j--) {str3 += list.get(j).toString();}System.out.println(ZH2_10(str3));}// 兩個(gè)二進(jìn)制的數(shù)字相加(轉(zhuǎn)換成十進(jìn)制的數(shù)字返回)public static void add(String str, String str1) {int k = Integer.parseInt(str);int k1 = Integer.parseInt(str1);System.out.println(k);System.out.println(k1);int d = 0, d1 = 0;while (k != 0 || k1 != 0) {d = k % 10;d1 = k1 % 10;if (d + d1 > 1) {list.add(10);} else if (d + d1 == 1) {list.add(1);} else if (d + d1 == 0) {list.add(0);}k = k / 10;k1 = k1 / 10;}}// 將二進(jìn)制的數(shù)字轉(zhuǎn)換為十進(jìn)制(自定義)public static int ZH2_10(String str) {int k = 0;int n = 0;for (int i = 0; i < str.length(); i++) {k += Math.pow(2, n);n++;}return k;}
}
3.代碼把16進(jìn)制表示的串轉(zhuǎn)換為3進(jìn)制表示的串
public class text8 {/*** 代碼把16進(jìn)制表示的串轉(zhuǎn)換為3進(jìn)制表示的串*/public static void main(String[] args) {System.out.println(jin_zhi_16_3("c"));}private static int getRealValue(char x) {if (x >= '0' && x <= '9')return x - '0';if (x >= 'a' && x <= 'f')return x - 'a' + 10;if (x >= 'A' && x <= 'F')return x - 'A' + 10;return 0;}public static String jin_zhi_16_3(String x) {int n = 0; // 累加真值for (int i = 0; i < x.length(); i++) {n = n + getRealValue(x.charAt(i)); // 填空}String t = "";for (;;) {if (n == 0)break;t = (n % 3) + t;n = n / 10; // 填空}return t;}}
總結(jié)
- 上一篇: 文本框可编辑查看页面
- 下一篇: 将request中的所有参数存放到自定义