문제풀이/백준
2309 - 일곱 난쟁이
동바리
2021. 6. 22. 23:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <algorithm> | |
using namespace std; | |
int main(void) | |
{ | |
int dwarf[9] = {0}; | |
int dwarfSum = 0; | |
for(int i = 0; i < 9; i++) { | |
scanf("%d", &dwarf[i]); | |
dwarfSum += dwarf[i]; | |
} | |
sort(dwarf, dwarf + 9); | |
for(int i = 0; i < 9; i++) { | |
for(int j = i + 1; j < 9; j++) { | |
if(dwarfSum - dwarf[i] - dwarf[j] == 100) { | |
for(int k = 0; k < 9; k++) { | |
if(k == i || k == j) { | |
continue; | |
} | |
printf("%d\n", dwarf[k]); | |
} | |
return 0; | |
} | |
} | |
} | |
return 0; | |
} |
같은 배열에 대해서 합을 구할때는
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
}
}
이렇게 됨을 이해하자 j 를 i로 초기화한다.
1
2
3
4
5
세로로 이해하면 쉽다. 똑같이 i =0, j =0 초기화하면 더했던것을 또 더해본다.