蓝桥杯 Python 组省赛夺奖班-3.2 链表

一、自行车停放

题目

请添加图片描述

思路

  • list模拟
  • 链表模拟

代码

list模拟
import time
n = int(input())
a = []
a.append(int(input()))
##t1 = time.perf_counter()
for i in range(n-1):
    x,y,z = list(map(int,input().split()))
    y_index = a.index(y)
    if z == 0:
        a.insert(y_index,x)
    else:
        a.insert(y_index+1,x)
for i in range(len(a)):
    print(a[i],end = " ")
##t2 = time.perf_counter()
##print(f"UsedTime:{t2-t1}")

链表模拟

import time
class Node:
    def __init__(self,value):
        self.value = value
        self.next = None
class linknode:
    def __init__(self,node):
        self.head = Node(None)
        self.head.next = node
    def leftinsert(self,loc,value):
        a = Node(value)
        b = self.head
        while b.next.value != loc:
            b = b.next
        a.next = b.next
        b.next = a
    def rightinsert(self,loc,value):
        a = Node(value)
        b = self.head
        while b.value != loc:
            b = b.next
        a.next = b.next
        b.next = a
    def show(self):
        t = self.head.next
        while t != None:
            print(t.value,end = " ")
            t = t.next
        print("")

n = int(input())
f = int(input())
##t1 = time.perf_counter()
a = Node(f)
b = linknode(a)
for i in range(n-1):
    x,y,z = list(map(int,input().split()))
    if z == 0:
        b.leftinsert(y,x)
    else:
        b.rightinsert(y,x)
b.show()
##t2 = time.perf_counter()
##print(f"UsedTime:{t2-t1}")

二、约瑟夫环

题目

请添加图片描述

思路

使用list模拟或者使用循环链表实现

代码

list模拟
n,k,m = list(map(int,input().split()))
a = [i+1 for i in range(n)]
s = k-1
while(len(a) != 0):
    s = (s+m-1)%len(a)
    print(a.pop(s))

收获&感想:

  • 还是不够熟练
  • 有点粗心、脑子转不过来
  • 进度有点慢了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值