[๋ฐฑ์ค] ๐ฅ 1940 ์ฃผ๋ชฝ
๐ ๋์ด๋
๐ฅ Silver 4
๐ ๋ฌธ์
https://www.acmicpc.net/problem/1940
๐ ํ์ด
๋ถ๋ช
ํ Two Pointer
๋ฅผ ์ฌ์ฉํ์ฌ ํธ๋ ๋ฌธ์ ์ด์ง๋ง, ๊ทธ๋ ๊ฒ ์ํ๊ณ O(n x log(n)) ์ ๋์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง for
๋ฌธ์ ํตํด ํด๊ฒฐํ์๋ค.
๊ฐ๋จํ๊ฒ 0-n ๊น์ง์ i๊ฐ๊ณผ i-n ๊น์ง์ j๊ฐ์ ํตํ ์ด์ค for
๋ฌธ์ผ๋ก ํด๊ฒฐํ์๋ค.
์๋ฐ๋ก ๋ฌธ์ ๋ฅผ ํ์ด์ ์๊ฐ๋ณด๋ค ์๊ฐ์ด ๋๋ํ๋ ๊ฒ ๊ฐ๋ค.
๐ Code
package TwoPointer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class N1940 {
static int[] material;
static int count;
static int target;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
count = Integer.parseInt(br.readLine());
target = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
material = new int[count];
for (int i = 0; i < count; i++) {
material[i] = Integer.parseInt(st.nextToken());
}
MakeArmor();
}
public static void MakeArmor() {
int result = 0;
for (int i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) {
if (material[i] + material[j] == target) {
result += 1;
break;
}
}
}
System.out.println(result);
}
}
package (์ด๋ฆ); ๋ฅผ ๋๊ณ , class ์ด๋ฆ์ Main
์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
๊ฐ์ธ ๊ณต๋ถ ๊ธฐ๋ก์ฉ ๋ธ๋ก๊ทธ์
๋๋ค.
ํ๋ฆฌ๊ฑฐ๋ ์ค๋ฅ๊ฐ ์์ ๊ฒฝ์ฐ ์ ๋ณดํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.๐