백준 11557번 (파이썬)

Jiya·2025년 3월 11일


정답

T = int(input())
for t in range(T):
    N = int(input())
    result = []
    for n in range(N):
        data = input().split()
        result.append(data)
    max_value = max(int(item[1]) for item in result)
    
    for item in result:
        if int(item[1]) == max_value:
            print(item[0])
            break
  • input().aplit()은 따로 list로 감싸주지 않아도 list 형식이 된다는 걸 기억하자.
  • for 문에도 즉시 종료를 원하면 break를 쓸 수 있다.
  • int(item[1]) for item in result 에서 _ for item in result 가 먼저 실행되고 그 다음 int()가 실행 된다.

다음은 AI가 더 가독성 좋은 코드로 정리해 준 것이다.

T = int(input())
for t in range(T):
    N = int(input())
    schools = []
    consumptions = []
    
    for n in range(N):
        school, consumption = input().split()
        schools.append(school)
        consumptions.append(int(consumption))
    
    # 최대 소비량의 인덱스를 찾아 해당 학교 출력
    max_index = consumptions.index(max(consumptions))
    print(schools[max_index])

index()를 사용하여 좀 더 깔끔하게 정리.

profile
코딩 공부 노트

0개의 댓글