Блог пользователя KickItLikeShika

Автор KickItLikeShika, история, 4 года назад, По-английски

I'm not good at Python but i'm trying to learn it, when i always submit a solution to Codeforces i always get run time error, but if i made the same solution in c++ it's okay and accepted, and here is an example. This is the problem set: https://codeforces.com/problemset/problem/1206/B

and here is my solution, all is well in my terminal window but i get run time error even in the first test case, i submit PyPy 3.6 or Python 3.7.2, i tried both

n = int(input())
    # ls = []
    counter = 0
    i = 0
    for i in range(n):
        k = int(input())
        # ls.append(k)
        if k > 1:
            counter += (k - 1)
        elif k < -1:
            counter += abs((k + 1))
        else:
            counter += 1
    print(counter)

and thank you.

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Auto comment: topic has been updated by KickItLikeShika (previous revision, new revision, compare).

»
4 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Your problem is input. input() returns string — line of input. For example, first call in statement test returns "2" and second returns "-1 1". You can't make integer from the second line. It you want to get numbers from line of input you must read line, than split it and make every element of line an integer.

line = input().split()
for i in range(len(line)):
    line[i] = int(line[i])

shorter version of this code using python magic:

line = list(map(int, input().split()))