-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModTwo.java
More file actions
40 lines (36 loc) · 1.01 KB
/
ModTwo.java
File metadata and controls
40 lines (36 loc) · 1.01 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
package _Test;
public class ModTwo {
public int getTotalNum(int target, int range) {
int t = getBinForm(target);
int ans = 0;
for (int i = 0; i <= range; i++) {
int cur = getBinForm(i);
int add1 = t + cur;
int add2 = getBinForm(target + i);
if (add1 == add2) {
System.out.println(i);
ans++;
}
}
return ans;
}
private int getBinForm(int target) {
int ans = 0;
int move = 0;
while (target != 0) {
int cur = target % 10;
if ((cur & 1) == 1) { // 该位置是奇数
ans |= 1 << move;
}
move++;
target /= 10;
}
return ans;
}
public static void main(String[] args) {
int target = 23;
int ans1 = new ModTwo().getTotalNum(target, 9);
int ans2 = new ModTwo().getTotalNum(target, 99);
System.out.println("Ans: " + (ans2 - ans1));
}
}