当前位置: 首页 > news >正文

编程题记录3

九宫幻方

题目链接: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 = 1
    for j in range(9):
        if ls[j] != data_baoli[i][j] and ls[j] != 0:
            ok = 0
            break
    if ok:
        ans += 1
        outcome = 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 = 0
        flag_B = 1
        A_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 = 1
            flag_B = 0
        else:
            chupai.append(A_chupai)
        if A == []:
            break
    if flag_B:
        flag_B = 0
        flag_A = 1
        B_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 = 1
            flag_A = 0
        else:
            chupai.append(B_chupai)
    if B == []:
        break

if 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]) == '*':
                    continue
                position = qingwa.index('*')
                # 如果更新这只青蛙,更新后的坐标
                new_position = i + move
                # 如果新的坐标等于*的坐标,交换坐标,并生成新的排序
                if new_position == position:
                    new_paixu = qingwa
                    new_paixu[position], new_paixu[i] = new_paixu[i], new_paixu[position]
                    if new_paixu == obj_:
                        print(step + 1)
                        return
                    ok = False
                    # 判断新排序是否在Q的排序中
                    for j in range(len(Q)):
                        if new_paixu == Q[j][0]:
                            ok = True
                    if 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 datetime

date_str = input().strip()
A, B, C = map(int, date_str.split('/'))
ans = set()
def con_year(x):
    if x>=60:
        return x+1900
    else:
        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"))

相关文章:

  • 算法训练营第二十三天 | 贪心算法(一)
  • x-cmd install | Wuzz - Web 开发与安全测试利器,交互式 HTTP 工具
  • 《尘埃落定》读后感
  • Power BI嵌入应用:常见问题与调试技巧
  • endnote相关资料记录
  • Visual Studio Code 无法打开源文件解决方法
  • Java Spring Cloud应用全栈性能优化指南
  • 【jvm】垃圾回收的并行和并发
  • 内核编程九:进程概述
  • Buffer overFolw---Kryo序列化出现缓冲区溢出的问题解决
  • Spring Cache 实战指南
  • 华为机试牛客刷题之HJ58 输入n个整数,输出其中最小的k个
  • 掌握 Postman:高级 GET 请求技术与响应分析
  • Ubuntu22.04美化MacOS主题
  • 什么是正文化
  • 【CSS3】完整修仙功法
  • WordPress 代码高亮插件 io code highlight
  • 【C++】string类字符串详细解析
  • SCI英文论文Accepted后的第一步——Rights and Access
  • Jenkins 集成 SonarQube 代码静态检查使用说明
  • 多地征集农村假冒伪劣食品违法线索,全链条整治“三无”产品
  • 商务部:将积极会同相关部门加快推进离境退税政策的落实落地
  • 涨价应对关税变化是短期之策,跨境电商塑造新品牌开辟“新蓝海”
  • 对话|男篮国手杨瀚森:参加NBA选秀,去更大的舞台追梦
  • 体育公益之约跨越山海,雪域高原果洛孕育足球梦
  • 蚂蚁集团将向全体股东分红