zoukankan      html  css  js  c++  java
  • java十进制到(二进制,八进制,十六进制)的转换的优化

     1 import java.util.Scanner;
     2 
     3 public class ArrayTest {
     4 
     5     public static void main(String[] args) {
     6         int n, m;
     7         Scanner cin = new Scanner(System.in);
     8         n = cin.nextInt();
     9         m = cin.nextInt();
    10         toBin(n);
    11         toHex(m);
    12     }
    13 
    14     public static void toBin(int n) {// 十进制--->二进制
    15         tran(n, 1, 1);
    16     }
    17 
    18     public static void toHex(int n) {// 十进制--->十六进制
    19         tran(n, 15, 4);
    20     }
    21 
    22     public static void toBa(int n) {// 十进制--->八进制
    23         tran(n, 7, 3);
    24     }
    25 
    26     public static void tran(int n, int base, int offest) {
    27         if (n == 0) {
    28             System.out.println(0);
    29             return;
    30         }
    31         char[] ch = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
    32                 'B', 'C', 'D', 'E', 'F' };
    33         char[] str = new char[100];
    34         int pos = str.length;
    35         while (n != 0) {
    36             int temp = n & base;
    37             str[--pos] = ch[temp];
    38             n = n >>> offest;
    39 
    40         }
    41         for (int i = pos; i < str.length; i++) {
    42             System.out.println(str[i]);
    43         }
    44     }
    45 
    46 }
    View Code
  • 相关阅读:
    Uri编码,包括javascript前端与C#服务器端
    快速排序
    bootstrap
    boosting
    bagging
    SHELL排序
    冒泡排序
    插入排序
    选择排序
    二叉树的数学性质
  • 原文地址:https://www.cnblogs.com/sxmcACM/p/3460221.html
Copyright © 2011-2022 走看看