BOJ::2309 일곱 난쟁이
https://www.acmicpc.net/problem/2309
얼핏보면 9개의 숫자중 7개의 숫자를 뽑아야 하는 것 처럼 보이지만,
9C7 = 9C2 이므로,
1~9번 난쟁이의 키를 모두 더한 후 두 난쟁이의 키를 빼도 된다.
<JAVA>
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String args[]) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Integer> res = new ArrayList(); int a[] = new int[9]; int sum=0; int idx1=0,idx2=0; for(int i = 0 ; i < 9 ; i++){ a[i]=Integer.parseInt(br.readLine()); sum+=a[i]; } for(int i = 0 ; i < 9 ; i++){ for(int j = 0 ; j < 9 ; j++){ if(sum-a[i]-a[j]==100){ idx1=i; idx2=j; break; } } } for(int i = 0 ; i < 9 ; i++){ if(i==idx1 || i==idx2){ continue; } res.add(a[i]); } Collections.sort(res); for(int v : res){ System.out.println(v); } } } | cs |
<C++>
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 | #include <iostream> #include <algorithm> using namespace std; int a[9]; int res[7]; int main() { int sum = 0; for (int i = 0; i < 9; i++) { cin >> a[i]; sum += a[i]; } int idx1 = 0, idx2 = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (sum - a[i] - a[j] == 100) { idx1 = i; idx2 = j; break; } } } int idx = 0; for (int i = 0; i < 9; i++) { if (i == idx1 || i == idx2) { continue; } res[idx] = a[i]; idx++; } sort(res, res + 7); for (auto &v : res) { cout << v << "\n"; } } | cs |