코딩딩딩
(python) 백준 1759 - 암호 만들기 본문
https://www.acmicpc.net/problem/1759
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
1. 문제 설명
입력받은 문자를 사용하여 최소 한 개의 모음과 최소 두 개의 자음으로 구성할 수 있는 암호를 출력하는 문제
2. 문제 풀이
문자를 리스트에 입력받고 오름차순으로 정렬한다.
그리고 combinations함수를 사용하여 서로 다른 l개를 선택할 수 있는 경우의 수 저장한다.
password_list = list(input().split())
password_list.sort() #오름차순 정렬
combination_password_list = combinations(password_list, l) #l개 만큼 조합 선택
각 경우의 수별로 자음의 개수와 모음의 개수를 확인하면서 조건 성립시 해당 암호를 출력한다.
for password in combination_password_list:
num_vowel = 0 #모음 개수
num_consonant = 0 #자음 개수
for i in range(len(password)): #자음,모음 개수 검사
if password[i] == 'a' or password[i] == 'e' or password[i] == 'i' or password[i] == 'o' or password[i] == 'u':
num_vowel += 1
else:
num_consonant += 1
if num_vowel >= 1 and num_consonant >= 2: #암호 성립 조건
print(''.join(password))
3. 전체 코드
from itertools import combinations
l, c = map(int, input().split())
password_list = list(input().split())
#오름차순 정렬
password_list.sort()
#l개 만큼 조합 선택
combination_password_list = combinations(password_list, l)
for password in combination_password_list:
num_vowel = 0 #모음 개수
num_consonant = 0 #자음 개수
#자음,모음 개수 검사
for i in range(len(password)):
if password[i] == 'a' or password[i] == 'e' or password[i] == 'i' or password[i] == 'o' or password[i] == 'u':
num_vowel += 1
else:
num_consonant += 1
#암호 성립 조건
if num_vowel >= 1 and num_consonant >= 2:
print(''.join(password))
'백준' 카테고리의 다른 글
(python) 백준 2292 - 벌집 (0) | 2023.01.30 |
---|---|
(python) 백준 1912 - 연속합 (0) | 2023.01.28 |
(python) 백준 6603 - 로또 (0) | 2023.01.23 |
(python) 백준 1546 - 평균 (0) | 2023.01.21 |
(python) 백준 1157 - 단어 공부 (0) | 2023.01.21 |
Comments