2025年6月25日 星期三

python 縮格練習

 n =int(input())

s = list(range(n))

f = [int(i) for i in input().split()]


print(s)

print(f)

print()


print('正確')

v = []

for i in s:

  if i not in v:

    cur =i

    g = []

    while cur not in g:

      v.append(cur)

      g.append(cur)

      cur = f[cur]

  

    print(g)


print('錯誤1')

v = []

for i in s:

  if i not in v:

    cur =i

    g = []

    while cur not in g:

      v.append(cur)

      g.append(cur)

      cur = f[cur]

  

      print(g)


print('錯誤2')

v = []

for i in s:

  if i not in v:

    cur =i

    g = []

    while cur not in g:

      v.append(cur)

      g.append(cur)

      cur = f[cur]

  

  print(g)


print('錯誤3')

v = []

for i in s:

  if i not in v:

    cur =i

    g = []

    while cur not in g:

      v.append(cur)

      g.append(cur)

      cur = f[cur]


print(g)


10

4 7 2 9 6 0 8 1 5 3


STDIN

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[4, 7, 2, 9, 6, 0, 8, 1, 5, 3]


Output:

正確

[0, 4, 6, 8, 5]

[1, 7]

[2]

[3, 9]

錯誤1

[0]

[0, 4]

[0, 4, 6]

[0, 4, 6, 8]

[0, 4, 6, 8, 5]

[1]

[1, 7]

[2]

[3]

[3, 9]

錯誤2

[0, 4, 6, 8, 5]

[1, 7]

[2]

[3, 9]

[3, 9]

[3, 9]

[3, 9]

[3, 9]

[3, 9]

[3, 9]

錯誤3

[3, 9]


沒有留言:

張貼留言