# 求解(Python)!!! 飆程式網 第三級 第十題 神奇的數字轉輪?

### 2 個解答

• 6 個月前
最佳解答

# !python3

def nxt(lst):

j = len(lst)

for i in range(len(lst)):

if 9 > lst[i]:

lst[i] += 1

break

else:

lst[i] = 0

return

#

def isSame(lst0, lst1):

if len(lst0) != len(lst1): return False

for i in range(len(lst1)):

if lst0[i] != lst1[i]: return False

return True

#

def appRule(st, ru, cn):

tp = st[:]

for i in range(len(cn)):

c = cn[i]

if c > 0:

for j in range(len(tp)):

tp[j] += (ru[i][j]*c)

tp[j] %= 10

return tp

#

def value(lst): return sum(lst)

#

nV = int(input())

nR = int(input())

rList = []

for i in range(nR):

rList.append([int(x) for x in input().split()])

line = input()

st = [int(x) for x in line[0:nV]]

en = [int(x) for x in line[1+nV:]]

cont = [0 for x in range(nR)]

last = [9 for x in range(nR)]

solu = []

valu = value(last) + 1

while True:

if valu > value(cont): # test only better solutions

tmp = appRule(st, rList, cont)

if isSame(tmp, en):

solu = cont[:]

valu = value(cont)

if isSame(cont, last): break # stop when reaching the end

nxt(cont)

#

print(value(solu))

• 登入以對解答發表意見
• 6 個月前

import sys

while(sInput!=""):

sInput=sInput.replace("\r","").replace("\n","")

sInput.strip()

listTime=sInput.split()

iHour1=int(listTime[0])

iMin1=int(listTime[1])

sInput=sInput.replace("\r","").replace("\n","")

sInput.strip()

listTime2=sInput.split()

iHour2=int(listTime2[0])

iMin2=int(listTime2[1])

iTotal=0

if iHour1==iHour2:

if iMin2>=iMin1:

print("0",end=" ")

print(iMin2-iMin1)

else:

iTotal=((24+iHour2)*60+iMin2)-(iHour1*60+iMin1)

print(iTotal//60,end=" ")

print(iTotal-(iTotal//60)*60)

elif iHour2<iHour1:

iTotal=((24+iHour2)*60+iMin2)-(iHour1*60+iMin1)

print(iTotal//60,end=" ")

print(iTotal-(iTotal//60)*60)

else:

iTotal=(iHour2*60+iMin2)-(iHour1*60+iMin1)

print(iTotal//60,end=" ")

print(iTotal-(iTotal//60)*60)