Python/USACO

USACO 2020 January Contest, Bronze problem1

나누기 2021. 9. 29. 05:39

http://www.usaco.org/index.php?page=viewproblem2&cpid=987&lang=en 

 

USACO

Bessie the cow is working on an essay for her writing class. Since her handwriting is quite bad, she decides to type the essay using a word processor. The essay contains $N$ words ($1\le N\le 100$), separated by spaces. Each word is between 1 and 15 charac

www.usaco.org

Python3 solution 제공된 테스트 케이스를 모두 통과하였습니다. 

# input
N, K = map(int,input().split())
lis = input().split()
# parameter
i = 0
templ = # temporary length
temp = []
temps = ''

while i < N:
  templ += len(lis[i]) # K 조건을 맞추기 위함.
    if templ > K:
      for x in temp:
        temps += x+' ' # 출력 형식을 맞추기 위함.
      print(temps)
      temps = ''
      templ = 0
      temp = []
    else:
      temp.append(lis[i]) # 출력을 위해 요소를 모음.
      i += 1

# 반복문이 끝난 후 else 구문에서 마칠 시 출력이 되지 않기 때문에 temp에 모인 요소가 있다면 출력.
if len(temp) != 0:
  for x in temp:
    temps += x + ' '
  print(temps)

반응형