-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q41.java
50 lines (33 loc) · 1.4 KB
/
Q41.java
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
45
46
47
48
49
50
import static util.util_string.*;
public class Q41 {
public static void main (String[] args) {
int ans = 0;
for (int left = 0; left < 256; left++) {
if (is_exist_duplicate_digits(left + "")) continue;
String left_bs = Integer.toBinaryString(left);
while (left_bs.length() < 8) left_bs = "0" + left_bs;
String left_bs_rev = reverse(left_bs);
int left_rev_2_to_10 = Integer.parseInt(left_bs_rev, 2);
if (is_exist_duplicate_digits(left + "" + left_rev_2_to_10)) continue;
for (int right = 0; right < 256; right++) {
if (is_exist_duplicate_digits(right + "")) continue;
String right_bs = Integer.toBinaryString(right);
while (right_bs.length() < 8) right_bs = "0" + right_bs;
String right_bs_rev = reverse(right_bs);
int right_rev_2_to_10 = Integer.parseInt(right_bs_rev, 2);
String ip_address = left + "" + right + right_rev_2_to_10 + left_rev_2_to_10;
if (ip_address.length() != 10) continue;
if (is_exist_duplicate_digits(ip_address)) continue;
System.out.println(++ans + "\t" + left + "." + right + "." + right_rev_2_to_10 + "." + left_rev_2_to_10);
}
}
System.out.println(ans);
}
static boolean is_exist_duplicate_digits (String str) {
int[] seq = new int[10];
char[] c = str.toCharArray();
for (int i = 0; i < c.length; i++)
if (++seq[c[i] - '0'] > 1) return true;
return false;
}
}