| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- poetry
- 프로그래머스
- 2주차
- WHERE절
- github
- 프로젝트
- 개발일지
- Class
- js
- 백준
- REDIS
- 미니프로젝트
- Git
- channels
- Commpot
- WIL
- vscode
- resnet50
- 정보처리기사실기
- 채팅
- 1주차
- re-id
- sql
- 파이썬
- 정보처리기사
- 마스킹
- WebSocket
- 장고
- 알고리즘
- 가상환경
Archives
- Today
- Total
개발일기
#14 백준 코딩은 체육과목이다 & 블랙잭 본문
오늘은 백준에서 두 문제를 풀었다.
1. 코딩은 체육과목이다.

문자열을 이용해서 문제를 간단히 해결할 수 있었다.
import sys
input = sys.stdin.readline
number = int(int(input())/4)
print("long "*number+"int")
2. 블랙잭

import sys
input = sys.stdin.readline
cnt, sums = map(int, input().split())
numbers = list(map(int,input().split()))
result = 0
target = 0
for i in range(cnt-2):
for j in range(i+1, cnt-1):
for k in range(j+1, cnt):
if numbers[i] + numbers[j] + numbers[k] > sums:
continue
else:
result = numbers[i] + numbers[j] + numbers[k]
if target <= result:
target = result
print(target)
3개의 카드를 뽑아야하고, 모든 경우를 비교해봐야해서 3중 for문을 이용해서 코드를 짰다.
3중 for문을 돌면서 겹치지 않게 범위를 지정해주었다.
for문을 이용한 풀이 말고도, combination을 이용해 조합으로 푼 풀이가 있다.
개인적으로 이 풀이 방법이 더 빠르고 코드도 간결하여 좋았다.
# combination을 활용한 풀이
from itertools import combinations
import sys
input = sys.stdin.readline
cnt, sums = map(int, input().split())
numbers = list(map(int,input().split()))
target = 0
for i in combinations(numbers,3):
temp_sum = sum(i)
if target <= temp_sum <= sums:
target = temp_sum
print(target)

'오류 및 알고리즘정리본' 카테고리의 다른 글
| 백준 알고리즘 시간초과 방지하는 법 (0) | 2023.05.04 |
|---|---|
| #15 백준 통계학 & 영단어 암기는 괴로워 (0) | 2023.05.04 |
| #13 프로그래머스 나머지가 1이 되는 수 찾기 & 없는 숫자 더하기 (0) | 2023.05.01 |
| #12 프로그래머스 비밀지도 (0으로 문자열 채우기) (0) | 2023.04.28 |
| #11 백준 영수증/색종이/팰린드롬인지 확인하기 (0) | 2023.04.27 |