https://www.acmicpc.net/problem/2309
Algorithm classification : Sorting, Brute-force Search
02.02.1. Problem
A crisis came to Snow White, who was living peacefully with the seven dwarfs to escape the queen. There were nine dwarfs, not seven, who returned from work. All nine dwarfs claimed to be the protagonists of "Snow White and the Seven Dwarfs." Snow White, who had great mathematical intuition, remembered that, fortunately, the sum of the heights of the seven dwarfs was 100. Given the height of the nine dwarfs, write a program to help Snow White find the seven dwarfs.
02.02.2. Input limit
The heights of the dwarfs are given over nine rows. The given key is a natural number that does not exceed 100, the heights of the nine dwarfs are all different, and if there are several possible answers, any one is output.
02.02.3. Output
Print the heights of the seven dwarfs in ascending order. There is never a case where the seven dwarfs cannot be found.
02.02.4. Solution
for(i = 0; i < 9, sum != 0; i++){ //check 변수를 따로 둬도 됨
for(j = i + 1; j < 9; j++){
if(sum == data[i] + data[j]){
sum -= (data[i] + data[j]);
data[i] = data[j] = 0;
break;
}
}
}
물론 if문에서 sum+100을 비교해도 됩니다. 하지만 sum+100을 계속하는 것보다 sum과 바로 비교하는 것이 경제적이겠죠. 그래서 반복문 이전에 sum -= 100을 해주는 것도 좋습니다.
sort(data, data+9);
for(i = 2; i < 9; i++)
printf("%d\n", data[i]);
그 다음은 뭐 정렬해서 출력해주시면 되겠죠. 크게 어렵지 않은 문제였습니다. 반복문과 조건문만 배우면 도전할 수 있는 문제죠.
'학부공부 > Algorithm PS_알고리즘' 카테고리의 다른 글
02.06. 숫자 야구[Brute-force Search][백준 2503] (0) | 2021.11.01 |
---|---|
02.05. 유레카 이론[Brute-force Search][백준 10448] (0) | 2021.11.01 |
02.04. 사탕게임[Brute-force Search][백준 3085] (0) | 2021.10.27 |
02.03. 분해합[Brute-force Search][백준 2231] (0) | 2021.10.27 |
02.01. 연속합[Brute-force Search][백준 1912] (0) | 2021.10.27 |
댓글