-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCBnumber.java
More file actions
47 lines (43 loc) · 1.56 KB
/
Copy pathCBnumber.java
File metadata and controls
47 lines (43 loc) · 1.56 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
45
46
47
package StringAssignment;
import java.util.Scanner;
public class CBnumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
String s = sc.next();
int[] cbNumbers = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
boolean[] visited = new boolean[size];
int count = 0;
for (int length = 1; length <= size; length++) {
for (int start = 0; start <= size - length; start++) {
String subStr = s.substring(start, start + length);
long num = Long.parseLong(subStr);
if (num <= 1) continue;
boolean isCB = true;
for (int i = 0; i < cbNumbers.length; i++) {
if (num == cbNumbers[i] || num % cbNumbers[i] == 0) {
if (num != cbNumbers[i]) isCB = false;
break;
}
}
if (isCB) {
boolean used = false;
for (int i = start; i < start + length; i++) {
if (visited[i]) {
used = true;
break;
}
}
if (!used) {
for (int i = start; i < start + length; i++) {
visited[i] = true;
}
count++;
}
}
}
}
System.out.println(count);
sc.close();
}
}