九宮幻方
題目鏈接:https://www.lanqiao.cn/problems/100/learning/?page=1&first_category_id=1&second_category_id=3&tags=%E7%9C%81%E8%B5%9B&tag_relation=intersection
先旋轉、鏡像得到所有的情況,可以發現情況是可以暴力得出的。接著就好辦了,只需要對比就可以了。
import os
import sys# 請在此輸入您的代碼 data_baoli = [[4,9,2,3,5,7,8,1,6],[2,7,6,9,5,1,4,3,8],[6,1,8,7,5,3,2,9,4],[8,3,4,1,5,9,6,7,2],[8,1,6,3,5,7,4,9,2],[2,9,4,7,5,3,6,1,8],[6,7,2,1,5,9,8,3,4],[4,3,8,9,5,1,2,7,6]]ls = list()
for _ in range(3):ls1, ls2, ls3 = map(int, input().split())ls.append(ls1)ls.append(ls2)ls.append(ls3)
# print(ls)
ans = 0
for i in range(len(data_baoli)):ok = 1for j in range(9):if ls[j] != data_baoli[i][j] and ls[j] != 0:ok = 0breakif ok:ans += 1outcome = data_baoli[i]
# print(outcome)
if ans == 1:for i in range(3):print(outcome[3*i], outcome[3*i+1], outcome[3*i+2])
else:print('Too Many')
拉馬車
題目鏈接:
https://www.lanqiao.cn/problems/101/learning/?page=1&first_category_id=1&second_category_id=3&tags=%E7%9C%81%E8%B5%9B&tag_relation=intersection
按照出牌規則模擬寫代碼即可。容易漏的是,當A空的時候,并且桌子上沒有可以獲得的牌子的時候就已經可以跳出來了,B不用再出牌,沒跳出來的話,B就會少了一張牌子,導致沒通過。這道題的測試用例沒有出現不能贏的情況,因此-1的輸出情況不用考慮。
import os
import sys# 請在此輸入您的代碼
A = list(input())
B = list(input())
# print(A,B)
flag_A = 1 # A先出牌
flag_B = 0
chupai = list()
while A != [] and B != []:if flag_A:flag_A = 0flag_B = 1A_chupai = A.pop(0)if A_chupai in chupai:# 找到出的牌子在序列中的位置position = chupai.index(A_chupai)chupai.append(A_chupai)huo_paizi = chupai[position:]A.extend(huo_paizi[::-1])# 更新牌chupai = chupai[:position]flag_A = 1flag_B = 0else:chupai.append(A_chupai)if A == []:breakif flag_B:flag_B = 0flag_A = 1B_chupai = B.pop(0)if B_chupai in chupai:# 找到出的牌子在序列中的位置position = chupai.index(B_chupai)chupai.append(B_chupai)huo_paizi = chupai[position:]B.extend(huo_paizi[::-1])# 更新牌chupai = chupai[:position]flag_B = 1flag_A = 0else:chupai.append(B_chupai)if B == []:breakif A:print(''.join(A))
elif B:print(''.join(B))
青蛙跳杯子
題目鏈接:https://www.lanqiao.cn/problems/102/learning/?page=1&first_category_id=1&second_category_id=3&tags=%E7%9C%81%E8%B5%9B,2017&tag_relation=intersection
import os
import sys# 請在此輸入您的代碼
source_ = list(input())
obj_ = list(input())
moves = [-1, 1, 2, -2, 3, -3]
# 存儲每一種可能的情況和對應的步數
Q = [list([source_, 0])]
def bfs():# 遍歷每一種可能的排序for v in Q:# 遍歷樹的每一種可能情況for move in moves:# qingwa = v[0][:]step = v[1]# 遍歷每一只青蛙for i in range(len(v[0])):qingwa = v[0][:]if str(qingwa[i]) == '*':continueposition = qingwa.index('*')# 如果更新這只青蛙,更新后的坐標new_position = i + move# 如果新的坐標等于*的坐標,交換坐標,并生成新的排序if new_position == position:new_paixu = qingwanew_paixu[position], new_paixu[i] = new_paixu[i], new_paixu[position]if new_paixu == obj_:print(step + 1)returnok = False# 判斷新排序是否在Q的排序中for j in range(len(Q)):if new_paixu == Q[j][0]:ok = Trueif ok is False:Q.append([new_paixu, step+1])bfs()
代碼通過率為66.7%,剩下的超時了,待優化。
日期問題
題目鏈接:https://www.lanqiao.cn/problems/103/learning/?page=1&first_category_id=1&second_category_id=3&tags=%E7%9C%81%E8%B5%9B&tag_relation=intersection
不采用try…except…會報段錯誤。
import os
import sys# 請在此輸入您的代碼
from datetime import datetimedate_str = input().strip()
A, B, C = map(int, date_str.split('/'))
ans = set()
def con_year(x):if x>=60:return x+1900else:return x+2000
# 年月日
try:y = con_year(A)dt = datetime(y, B, C)if datetime(1960,1,1)<=dt<=datetime(2059,12,31):ans.add(dt)
except ValueError:pass# 月日年
try:y = con_year(C)dt = datetime(y, A, B)if datetime(1960,1,1)<=dt<=datetime(2059,12,31):ans.add(dt)
except ValueError:pass# 日月年
try:y = con_year(C)dt = datetime(y, B, A)if datetime(1960,1,1)<=dt<=datetime(2059,12,31):ans.add(dt)
except ValueError:pass
# print(ans)for dt in sorted(ans):print(dt.strftime("%Y-%m-%d"))