蓝桥杯(Python)每日练Day5

题目

OJ1229

题目分析

  1. 题目完全符合栈的特征,后进先出。
  2. 如果能够熟练使用列表的9种方法那么这道题很容易解出。

题解

python 复制代码
a=[]#存衣服
n=int(input())
for i in range(n):
  l=list(input().split())#判断每一步的操作
  if len(l[0])==2:
    a.append(l[1])
  else:
    while a.pop()!=l[1]:pass
if len(a)==0:print('Empty')
else:print(a[-1])

题目

题目分析

  1. 输入一层就记录一层的权和,然后输出权和最大的层数,第一步,判断总共有多少层。
python 复制代码
以下是 log() 方法的语法:
import math
math.log(x)
math.log(x,base)#以base为底x的对数
参数
x -- 数值表达式。
base -- 基底
则对此题总共有math.log(n,2)+1层
  1. 利用列表初始化两个数组,一个用x存储每一个节点的权,s记录每一层的和。
  2. 利用列表的切片分层求权的和。
  3. 输出s最大的值的索引。

题解

python 复制代码
import math
s=[]
n = int(input())
x= list(map(int, input().split()))
deep=int(math.log(n,2))+1
for i in range(deep):
    s.append(sum(x[2**i-1:2**i+2**i-1]))
print(s.index(max(s))+1)
相关推荐
阿尔的代码屋5 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng82 天前
Python+Django+H5+MySQL项目搭建
python·django