-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
44 lines (37 loc) · 1.33 KB
/
Main.java
File metadata and controls
44 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
if (s == null) return;
String str = s.substring(2, s.length()); //截取16进制字符串
int num = 0;
int count = 0;
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) == 'a') {
num += 10 * (Math.pow(16, count));
} else if (str.charAt(i) == 'b') {
num += 11 * (Math.pow(16, count));
} else if (str.charAt(i) == 'c') {
num += 12 * (Math.pow(16, count));
} else if (str.charAt(i) == 'd') {
num += 13 * (Math.pow(16, count));
} else if (str.charAt(i) == 'e') {
num += 14 * (Math.pow(16, count));
} else if (str.charAt(i) == 'f') {
num += 15 * (Math.pow(16, count));
} else {
int cur = str.charAt(i) - '0';
num += cur * (Math.pow(16, count));
}
count++;
}
int result = 0;
String s1 = Integer.toBinaryString(num);
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == '1') result++;
}
System.out.println(result);
}
}