일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 입이 트이는 영어
- EBS어학당
- 1793번
- BinaryGap
- github
- 1057번
- 2630번
- 프로그래머스
- 분할정복
- 영어
- WebSecurityConfigurerAdapter
- codility
- SecurityFilterChain
- 신규아이디추천
- Java
- 알고리즘
- 18406번
- 백준
- 권주현의 진짜 영국 영어
- 11727번
- Spring Security
- 11047번
- caniuse
- python
- 1759번
- 파이썬
- 1992번
- 2163번
- programmers
- 9251번
Archives
- Today
- Total
철갑이의 이모저모
[백준] 2309(일곱 난쟁이) with java 본문
728x90
문제
https://www.acmicpc.net/problem/2309
2309번: 일곱 난쟁이
아홉 개의 줄에 걸쳐 난쟁이들의 키가 주어진다. 주어지는 키는 100을 넘지 않는 자연수이며, 아홉 난쟁이의 키는 모두 다르며, 가능한 정답이 여러 가지인 경우에는 아무거나 출력한다.
www.acmicpc.net
풀이
1. 모든 난쟁이의 키를 합해줍니다.
2. 7명의 난쟁이 키의 합이 100이기 때문에 즉,
(9명 난쟁이 키의 합) - (2명 가짜 난쟁이 키의 합) = 100
이라고 볼 수 있습니다.
분명 맞은 것 같은데 계속 틀려서 고민을 했습니다..
▼ 그래서 찾은 반례
더보기
20
7
23
19
10
15
24
8
답
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] arr = new int[9];
int sum = 0;
// 9명의 난쟁이 키의 합
for (int i = 0; i < 9; i++) {
arr[i] = Integer.parseInt(br.readLine());
sum += arr[i];
}
// 9명 키의 합에서 2명 키를 뺐을때 100 이면 break
for (int i = 0; i < 8; i++) {
for (int j = i + 1; j < 9; j++) {
if (sum - (arr[i] + arr[j]) == 100) {
arr[i] = 101;
arr[j] = 101;
i = 8;
break;
}
}
}
Arrays.sort(arr);
// 7 명의 난쟁이 키 출력 (조건문으로 101이 아닌 키를 출력해도 됨)
for (int i = 0; i < 7; i++) {
bw.write(arr[i] + "\n");
}
bw.flush();
bw.close();
}
}
728x90
'알고리즘' 카테고리의 다른 글
[codility] CyclicRotation with Python (0) | 2022.04.14 |
---|---|
[codility] BinaryGap with Python (0) | 2022.04.11 |
[Programmers] 체육복 with Python (0) | 2022.01.31 |
[Programmers] 숫자 문자열과 영단어 with Python (0) | 2022.01.29 |
[Programmers] 신규 아이디 추천 with Python (0) | 2022.01.29 |