강의

멘토링

커뮤니티

Inflearn Community Q&A

hjinnny3339's profile image
hjinnny3339

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

소스코드 관련 질문이 있습니다.

Written on

·

266

0

안녕하세요 :)
섹션 2. 6번 자릿수의 합에서 소스코드 관련하여 질문드립니다.
 
문제에서는 자릿수의 합이 같은 경우 입력순으로 먼저인 숫자가 출력이 된다고 되어있는데,
강의에서 풀이해주신 부분은 마지막에 입력된 숫자가 출력이 되는것 같습니다.
 
max = -2147000000
for x in a :
tot = digit_sum(x)
if tot>max:
max = tot
res = x
print(res)
 
 
input1
7 137 460 603 40 521 128 125
>> 해당 텍스트로 인풋값을 넣었을 때 137이 출력이 되어야하는데
선생님이 설명해주신 코드로는 128이 출력이 되어, 혹시 코드가 변경되어야하는게 아닐까요?
 
 
 
 
python코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

제가 드린 코드로 실행해보니 137이 출력됩니다. 

import sys
sys.stdin=open("in1.txt", "r")
def digit_sum(x):
    sum=0
    while x>0:
        sum+=x%10
        x=x//10
    return sum

n=int(input())
a=list(map(int, input().split()))
res=0
max=-2147000000
for x in a:
    tot=digit_sum(x)
    if tot>max:
        max=tot
        res=x
print(res)

 

 

 

hjinnny3339's profile image
hjinnny3339

asked

Ask a question