笔试-座位调整

应用

疫情期间课堂的座位进行了特殊的调整,不能出现两个同学紧挨着,必须隔至少一个空位。

给你一个整数数组desk表示当前座位的占座情况,由若干0和1组成,其中0表示没有占位,1表示占位。在不改变原有座位秩序情况下,还能安排坐几个人?

输入:1 0 0 0 1

第一行是个桌子数组表示作为占座情况,由若干0和1组成,其中0表示没有占位,1表示占位

输出:1

数值表示还能坐几个人

备注:1 ≤ desk.length ≤ 2*10^4

实现

python 复制代码
strs = input("请输入占座情况:").split()
desk = [int(i) for i in strs]

count = 0
for i in range(0, len(desk)):
    if desk[i] != 1:
      if i == 0 and desk[1] == 0:
        count += 1
      
      if i == len(desk)-1 and desk[-2] == 0:
        count += 1
      
      if i > 0 and i < len(desk)-1:
          if desk[i-1] == 0 and desk[i+1] == 0:
            count += 1
    
print(count)
powershell 复制代码
请输入占座情况:1 0 0 0 1
1
相关推荐
花酒锄作田3 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪7 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽7 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战7 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋13 小时前
[大模型实战 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