SW Expert Academy
SW ํ๋ก๊ทธ๋๋ฐ ์ญ๋ ๊ฐํ์ ๋์์ด ๋๋ ๋ค์ํ ํ์ต ์ปจํ ์ธ ๋ฅผ ํ์ธํ์ธ์!
swexpertacademy.com
์ค๋ช
์๋ก ๋ค๋ฅธ 7๊ฐ์ ์ ์ ์ค 3๊ฐ์ ์ ์๋ฅผ ๊ณจ๋ผ ํฉ์ ๊ตฌํด์ ์๋ฅผ ๋ง๋ค๋ ค๊ณ ํ๋ค.
์ด๋ ๊ฒ ๋ง๋ค ์ ์๋ ์ ์ค์์ 5๋ฒ์งธ๋ก ํฐ ์๋ฅผ ์ถ๋ ฅํ๋ค.
ํ์ด
์กฐํฉ(combinatinos) ์ฌ์ฉ
1. ๋ฐ๋ณต๋ฌธ์ผ๋ก 3๊ฐ์ฉ ์กฐํฉ์ ๋ง๋ค๊ณ ํฉ์ ๋ฆฌ์คํธ์ ์ ์ฅํ๋ค.
2. sorted๋ฅผ ์ฌ์ฉํด ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ์ ํ๊ณ set์ผ๋ก ์ค๋ณต์ ์ ๊ฑฐํ๋ค.
3. ๋ฆฌ์คํธ์ 5๋ฒ์งธ(์ธ๋ฑ์ค = 4)๋ฅผ ์ถ๋ ฅํ๋ค.
โ๏ธ combinations
combinations ํจ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ itertools ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ค.
from itertools import combinations
โ๏ธ combinations(๋ฆฌ์คํธ, ์ ์)
์ ์ ์๋ฆฌ์ 3์ ๋ฃ์ผ๋ฉด 3๊ฐ์ ์ซ์๋ก ์กฐํฉ์ ํ๋ค.
from itertools import combinations
n = [1, 2, 3, 4]
ans = list(combinations(n, 3))
print(ans)
#๊ฒฐ๊ณผ
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
itertools์๋ combinations ๋ง๊ณ ๋ ์์ด(permutations)์ด ์๋๋ฐ, ์กฐํฉ์ ์์๋ฅผ ๊ณ ๋ คํ์ง ์์ง๋ง ์์ด์ ์์๋ฅผ ๊ณ ๋ คํ๋ค.
permutaions ์ฌ์ฉ
from itertools import permutations
n = [1, 2, 3]
ans = list(permutations(n, 2))
print(ans)
#๊ฒฐ๊ณผ
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
combinations ์ฌ์ฉ
from itertools import combinations, permutations
n = [1, 2, 3]
ans = list(combinations(n, 2))
print(ans)
#๊ฒฐ๊ณผ
[(1, 2), (1, 3), (2, 3)]
โ๏ธ set(list) - ์ค๋ณต ์ ๊ฑฐ
n = [1, 1, 2, 2, 3]
print(set(n))
#๊ฒฐ๊ณผ
{1, 2, 3}
Solution
from itertools import combinations
t = int(input())
for tc in range(1, t+1):
n = list(map(int, input().split()))
ans = []
for i in combinations(n, 3): #list๋ก ์ ๋ฌถ์ด๋ ๋จ
ans.append(sum(i))
ans = list(set(sorted(ans, reverse=True))) #์ค๋ณต ์ ๊ฑฐ ํด์ผ ํจ - set ์ฌ์ฉ
print('#'+str(tc), ans[4])'๐งฉ Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [SWEA] 5431. ๋ฏผ์์ด์ ๊ณผ์ ์ฒดํฌํ๊ธฐ (Python/D3) (2) | 2023.11.16 |
|---|---|
| [SWEA] 1217. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 4์ผ์ฐจ - ๊ฑฐ๋ญ ์ ๊ณฑ (Python/D3) (0) | 2023.11.16 |
| [SWEA] 1234. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 10์ผ์ฐจ - ๋น๋ฐ๋ฒํธ (Python/D3) (0) | 2023.11.16 |
| [SWEA] 2070. ํฐ ๋, ์์ ๋, ๊ฐ์ ๋ (Python/D1) (2) | 2023.11.12 |
| [SWEA] 2071. ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ (Python/D1) (1) | 2023.11.12 |