九宫幻方
题目链接: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"))