close

給你一數字

e.g. 265

將這個數字當成 1. decimal; 2. hexadicimal

將decimal轉成binary string        = 100001001

將hexadecimal轉成binary string = 1001100101

第一個string 有三個bit 為 1

第二個string 有五個bit 為 1

輸出 3 5

----

package uva10019;

import java.util.Scanner;

/**
 *
 * @author awesq
 */
public class UVa10019 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int cases = sc.nextInt();
            while (cases-- > 0) {
                int dec = sc.nextInt();
                int hex = Integer.parseInt("" + dec, 16);
                String strDec = Integer.toBinaryString(dec);
                String strHex = Integer.toBinaryString(hex);

                int a = 0, b = 0;
                for (int i = 0; i < strDec.length(); i++) {
                    if (strDec.charAt(i) == '1') {
                        a++;
                    }
                }
                for (int i = 0; i < strHex.length(); i++) {
                    if (strHex.charAt(i) == '1') {
                        b++;
                    }
                }
                System.out.println("" + a + " " + b);
            }
        } catch (Exception e) {
        }
    }

}
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()