https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=960 困難度 ★ Java 內有提供轉二進位函數 1.Integer 類別有 static parseInt() 方法,可以將字串 (string) 中的整數轉換 int 型態的數值 (value) static int parseInt(String s, int radix) 可將字串解析為指定進位的整數 2.利用 Integer.bitCount計算出二進位位元數 詳細在->http://1010code.blogspot.tw/2016/07/q10019-funny-encryption-method_4.html
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for (int i = 0; i < n; i++) {
String str = scn.next();
System.out.printf("%d %d\n", Integer.bitCount(Integer.parseInt(str)),
Integer.bitCount(Integer.parseInt(str, 16)));
}
}
/*
題目:Q10019 : Funny Encryption Method
作者:1010
時間:西元 2016 年 7 月 */
}