Hi, how are you?
The problem is that the unauthorized reproduction is not possible, so to give you a brief explanation, it is a problem that adds the sum of the digits until it becomes one digit.
The first code I thought of is as follows.
T = int(input())
for test_case in range(1, T + 1):
f_n = int(input()) %9 # A value such as 588432 enters the input() position.
answer = f_n if f_n != 0 else 9
print(f"#{test_case} {answer}")
When I turned that code on my local piecham, there was no problem. But when I submit it to the site, I keep getting the problem timeout.
I was wandering and with the help of a friend who solved the problem, I fixed it as follows.
T = int(input())
answer = []
for test_case in range(1, T + 1):
f_n = int(input()) %9 # A value such as 588432 enters the input() position.
answer.append( f_n if f_n != 0 else 9 )
for test_case in range(1, T + 1):
print(f"#{test_case} {answer[test_case-1]}")
I appended the calculated value that comes out every time the for statement was turned to the list called answer, and when I wrote the for statement separately and printed it out, the execution time was greatly reduced.
What the hell is wrong with this?
No matter how much I think about it, the code below will take more time to append to the list and go around a separate for door I can't understand because the execution time is rather reduced.
python3 for list append print
The difference is... The number of times you create a variable or overwrite the value of the variable is different.
For example, if T
is 100
:
© 2024 OneMinuteCode. All rights reserved.