1215 회문1
파이썬에서 회문 관련 문제는 슬라이싱을 사용하는게 빠르다
for tc in range(10):
N = int(input())
cnt = 0
board = [list(input()) for _ in range(8)] # 가로 확인
board_rev = [[board[i][j] for i in range(8)] for j in range(8)] # 세로 확인
for row in range(8):
for s in range(8-N+1):
tmp = board[row][s:s+N]
if tmp == tmp[::-1]:
cnt += 1
tmp = board_rev[row][s:s+N]
if tmp == tmp[::-1]:
cnt += 1
print("#{} {}".format(tc+1, cnt))
1225 암호생성기
접근법 : 처음 주어지는 암호 배열의 초기값들이 사이클을 거듭할수록 어떤 값의 변화를 겪게 될 지 차근차근 생각해보면 규칙을 찾을 수 있음
for tc in range(10):
N = int(input())
result = list(map(int, input().split()))
possible_cycle = min(result)//15
cycle_done = [result[i]-15*(possible_cycle-1) for i in range(len(result))]
k = 0
while True:
k += 1
cycle_done.append(cycle_done.pop(0)-k)
if cycle_done[-1] <= 0:
cycle_done[-1] = 0
break
k %= 5
print("#{} {}".format(N," ".join(map(str,cycle_done[:-1]+[0]))))
# 위 코드는 다만 모든 수가 15보다 크다는 전제 하에 돌아간다
# 15보다 작은값이 있다면 그냥 while문 사용해서 푸는게 빠를 것
1289 원재의 메모리 복구하기
여러 방법으로 풀 수 있는 문제
- 방법 1 : 암호문 원본을 계속 바꿔나가면서 풀이하는 방식
- 방법 2 : 값이 바뀌어야 하는 순간(0에서 1로 변해야하거나 1에서 0으로 바꿔야할 때)에만 카운트(<- 이걸로 풀이)
(단 초기값은 항상 0임 - 초기화 되어있어서)
for tc in range(int(input())):
bits = input()
default = '0'
cnt = 0
for bit in bits:
if bit != default:
cnt += 1
default = bit
print("#{} {}".format(tc+1, cnt))
10505 소득불균형
마찬가지로 여러 방법이 있음
- 방법 1 : list comprehension으로 조건에 맞는 값만 구해서 길이 세기
- 방법 2 : 방법 1과 비슷하게 풀되 filter, lambda 식 활용하기
- 방법 3 : 딕셔너리(키,밸류) 활용해서 풀기 (+ 매번 있는지 확인하기 귀찮으니까 default dict 활용)
방법 1 & 2
# 방법 1
for tc in range(int(input())):
l = int(input())
income = [int(x) for x in input().split()]
avg = sum(income) / l
less = [x for x in income if x <= avg]
print("#{} {}".format(tc+1,len(less)))
# 방법 2
for tc in range(int(input())):
N = int(input())
data = list(map(int, input().split()))
print("#{} {}".format(tc+1, len(list(filter(lambda x: x <= sum(data)/N,data)))))
방법 3
from collections import defaultdict
for tc in range(int(input())):
N = int(input())
data = list(map(int, input().split()))
d = defaultdict(int)
total = 0
cnt = 0
for income in data:
d[income] += 1
total += income
total /= N
for k,v in d.items():
if k <= total:
cnt += v
print("#{} {}".format(tc+1, cnt))
'Problem Solving > SWEA' 카테고리의 다른 글
[ SWEA ] D3 - 1213, 1234, 1240, 3314, 4751 - python 문제풀이 (0) | 2021.07.02 |
---|---|
[ SWEA ] D3 - 1220, 1216, 5601, 10570 - python 문제풀이 (0) | 2021.07.01 |
[ SWEA ] D3 - 1217, 1230, 3431, 4406 - python 문제풀이 (0) | 2021.06.29 |
[ SWEA ] D2 - 1940, 1928, 1288, 1284, 1204 - python 문제풀이 (0) | 2021.06.28 |
[ SWEA ] D2 - 1954, 1948, 1946, 1945 - python 문제풀이 (0) | 2021.06.27 |
댓글