두 수의 합 (3273번)
https://www.acmicpc.net/problem/3273
풀이방법
a[i]
와a[j]
를 더한 값이 최대 2백만이므로, 배열의 크기를 2백만으로 주었음- 배열 인덱스에 입력받은 값을 주어 해당 인덱스에 1을 설정
x-i
를 하면i
일때x
가 되기 위한 값이 나오므로arr[x-i]
를 이용arr[i] == 0
인 경우 해당 값을 입력받지 않았다는 것이므로 패스x-i > i
는 문제 설명의a[i] + a[j] = x (1 ≤ i < j ≤ n)
을 만족하기 위한 조건
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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
int[] arr = new int[2_000_001];
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
int max = 0;
int index = 0;
for (int i=0; i<n; i++) {
index = Integer.parseInt(st.nextToken());
arr[index] = 1;
max = Math.max(max, index);
}
int x = Integer.parseInt(br.readLine());
int pairCount = 0;
for(int i=1; i<=max; i++) {
if (x-i > i && arr[i] != 0 && arr[x-i] == 1) {
pairCount++;
}
}
System.out.println(pairCount);
}
}
Comments powered by Disqus.