#31786: 這個為何吃RE?


harrison0423 (小龍)

學校 : 江翠國小
編號 : 180891
來源 : [111.240.106.173]
最後登入時間 :
2023-07-12 13:41:12
c291. APCS 2017-0304-2小群體 -- 2017年3月APCS | From: [180.217.244.218] | 發表日期 : 2022-08-19 17:08

def cycle(s):
  nums = [int(i) for i in s.split(' ')]
  n = len(nums)
  nc = 0
  check = [1 for i in range(n)]
  ct = ''
  for i in range(n):
    st = '('
    j = i 
    while (check[j] == 1):
      check[j] = 0
      st += str(j) + ' '     
      j = nums[j]
    else:
      if (len(st)>1):
        st += ')'
        print(st)
        nc += 1
  return nc
print(cycle(input()))
 
#31808: Re: 這個為何吃RE?


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
c291. APCS 2017-0304-2小群體 -- 2017年3月APCS | From: [39.8.167.128] | 發表日期 : 2022-08-20 16:36

def cycle(s):
  nums = [int(i) for i in s.split(' ')]
  n = len(nums)
  nc = 0
  check = [1 for i in range(n)]
  ct = ''
  for i in range(n):
    st = '('
    j = i 
    while (check[j] == 1):
      check[j] = 0
      st += str(j) + ' '     
      j = nums[j]
    else:
      if (len(st)>1):
        st += ')'
        print(st)
        nc += 1
  return nc
print(cycle(input()))

輸入第一行是人數

 
ZeroJudge Forum