#31905: 小新手ㄉpy解法


10946009@ntub.edu.tw (sd030)

學校 : 不指定學校
編號 : 196058
來源 : [114.24.159.139]
最後登入時間 :
2023-08-01 17:42:21
c085. 00350 - Pseudo-Random Numbers -- UVa350 | From: [1.164.241.32] | 發表日期 : 2022-08-27 16:42

一種是把被用過的L存到list裡去比對

case = 1
while 1:
  Z,I,M,L=list(map(int,input().split()))
  firstL = L
  if sum([Z,I,M,L]) == 0:
    break
  Ltotal = list()
  count = 0
  while L not in Ltotal:   #判斷L的數字是否已存在list裡面 
      Ltotal.append(L)     #沒有就把數字放進list,以便後續判斷
      count += 1
      L = (Z*L+I) % M
  if firstL == L:
    print(f"Case {case}: {count}")
  else:
    print(f"Case {case}: {count-1}")
  case += 1
 
一種是建立0~9999都放1的list,被用過的L    也就是Ltotal[L]設為0,當發現L再次被用過就跳出來print
case = 1
while 1:
  Z,I,M,L=list(map(int,input().split()))
  firstL = L
  if sum([Z,I,M,L]) == 0:
    break
  Ltotal = list()
  for i in range(0,9999):   #所有的Ltotal都變成1
    Ltotal.append(1)
  count = 0
  while Ltotal[L] != 0:    #判斷那個Ltotal的位置是否有被放東西過
      Ltotal[L] = 0           #還沒被放過的數字讓他變成0,代表已放置
      count += 1
      L = (Z*L+I) % M
  if firstL == L:
    print(f"Case {case}: {count}")
  else:
    print(f"Case {case}: {count-1}")
  case += 1
 
ZeroJudge Forum