-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path주몽.java
More file actions
61 lines (51 loc) · 1.65 KB
/
주몽.java
File metadata and controls
61 lines (51 loc) · 1.65 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package Kundol;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
//주몽d
public class K1N {
static int ret;
static int n, m;
static int k;
// 선택한 2개를 담을 원시 배열(박싱 제거)
static int[] pick = new int[2];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine().trim());
m = Integer.parseInt(br.readLine().trim());
// split 대신 StringTokenizer로 메모리 절약
StringTokenizer st = new StringTokenizer(br.readLine());
int[] num = new int[n];
for (int i = 0; i < n; i++) num[i] = Integer.parseInt(st.nextToken());
Arrays.sort(num);
int left = 0;
int right = num.length-1;
while(left < right) {
int sum = num[left] + num[right];
if(sum == m) {
ret += 1;
left+=1;
right-=1;
}else if (sum < m) {
left += 1;
} else {
right -= 1;
}
}
// combi(0, 0, 0, num); // start=0, depth=0, sum=0
System.out.println(ret);
}
// start: 다음에 고를 시작 인덱스, depth: 현재까지 뽑은 개수(0~2), sum: 현재 합
static void combi(int start, int depth, int sum, int[] num) {
if (depth == 2) { // 2개 뽑으면 합 확인하고 종료
if (sum == m) ret++;
return; // ★ 더 내려가지 않음 (필수)
}
for (int i = start; i < num.length; i++) {
pick[depth] = num[i];
combi(i + 1, depth + 1, sum + num[i], num);
}
}
}