Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db4cf8d

Browse files
이지영: [BOJ] 1644 소수의 연속합_250327
1 parent 4ff41b7 commit db4cf8d

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

‎BOJ/1000-5000번/JY_1644.java‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class JY_1644 {
4+
5+
static int N;
6+
static List<Integer> pList;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
11+
N = Integer.parseInt(br.readLine());
12+
pList = new ArrayList<>();
13+
findPrime();
14+
15+
// System.out.println(pList);
16+
17+
if(N == 1) {
18+
System.out.println(0);
19+
return;
20+
}
21+
int s = 0;
22+
int e = 0;
23+
int total = pList.get(e);
24+
int cnt = 0;
25+
while(s <= e) {
26+
if(total == N) {
27+
cnt++;
28+
total -= pList.get(s);
29+
s++;
30+
}
31+
else if(total > N) {
32+
total -= pList.get(s);
33+
s++;
34+
}
35+
else {
36+
e++;
37+
if(e >= pList.size()) break;
38+
total += pList.get(e);
39+
}
40+
}
41+
42+
System.out.println(cnt);
43+
44+
}
45+
public static void findPrime() {
46+
boolean[] isPrime = new boolean[N+1];
47+
Arrays.fill(isPrime, true);
48+
isPrime[0] = false;
49+
isPrime[1] = false;
50+
51+
52+
for(int i = 2; i <= Math.sqrt(N); i++){ // 2부터 n의 제곱근까지의 모든 수를 확인
53+
if(isPrime[i]){ // 해당수가 소수라면, 해당수를 제외한 배수들을 모두 false 처리하기
54+
for(int j = i*i; j<= N; j += i){//그 이하의 수는 모두 검사했으므로 i*i부터 시작
55+
isPrime[j] = false;
56+
}
57+
}
58+
}
59+
for(int i=2; i<N+1; i++) {
60+
if(isPrime[i]) pList.add(i);
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /