1008 Elevator (20 分) (巨水题)

本文展示了一段C++代码,用于读取整数序列,并计算相邻元素之间的差值。根据差值的正负,程序采用不同的加权方式计算总和。最后输出计算得到的总和。

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+100;
int q[maxn];
int main()
{
    int n;
    int ans=0;

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&q[i]);

    }
    for(int i=1;i<=n;i++)
    {

        int c=q[i]-q[i-1];
        if(c<0)
        {
            ans=ans-c*4+5;
        }
        else
        {
            ans=ans+c*6+5;
        }
    }
    cout<<ans<<endl;
}

library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.NUMERIC_STD.ALL; – 定义电梯控制器的实体 entity CON is Port ( clk : in STD_LOGIC; – 时钟信号 reset : in STD_LOGIC; – 复位信号 floor_requests : in STD_LOGIC_VECTOR(5 downto 0); – 楼层请求,每位对应一个楼层,1表示请求 open_door : in STD_LOGIC; – 开门按钮 close_door : in STD_LOGIC; – 关门按钮 emergency_stop : in STD_LOGIC; – 紧急制动按钮 direction : out STD_LOGIC; current_floor : out STD_LOGIC_VECTOR(3 downto 0); – 当前楼层,四位输出 target_floor : out STD_LOGIC_VECTOR(3 downto 0); reached_target : out STD_LOGIC; – 到达目标楼层信号 buzz : buffer STD_LOGIC; – 蜂鸣器 up_indicator : out STD_LOGIC; – 上行指示灯 down_indicator : out STD_LOGIC; – 下行指示灯 door_signal : buffer STD_LOGIC_VECTOR(1 downto 0) – 门状态信号及指示灯(01 关门,10 开门) ); end CON; – 定义电梯控制器的行为 architecture Behavioral of CON is signal current_floor_reg : STD_LOGIC_VECTOR(3 downto 0) := “0001”; – 当前楼层寄存器,初始值为1 signal target_floor_reg : STD_LOGIC_VECTOR(3 downto 0) := “0001”; – 目标楼层寄存器,初始值为1 signal moving : STD_LOGIC := ‘0’; – 电梯是否在移动,0表示停止,1表示移动 signal direction_reg : STD_LOGIC := ‘0’; – 电梯移动方向寄存器,0表示向上,1表示向下 signal reached_target_reg : STD_LOGIC := ‘0’; – 是否到达目标楼层寄存器 signal requests : STD_LOGIC_VECTOR(5 downto 0); – 楼层请求寄存器 signal door_opened : STD_LOGIC := ‘0’; – 门是否已经打开标志 begin – 电梯控制逻辑过程 process(clk, reset) begin if reset = ‘1’ then – 复位时,将所有寄存器重置为初始状态 current_floor_reg <= “0001”; target_floor_reg <= “0001”; moving <= ‘0’; direction_reg <= ‘0’; reached_target_reg <= ‘0’; requests <= (others => ‘0’); door_signal <= “01”; – 初始状态为关门 door_opened <= ‘0’; elsif rising_edge(clk) then – 处理开门和关门请求 if open_door = ‘1’ then buzz <= ‘1’; – 打开蜂鸣器 door_signal <= “10”; – 开门并点亮开门指示灯 door_opened <= ‘1’; – 标记门已打开 elsif close_door = ‘1’ then buzz <= ‘0’; – 关闭蜂鸣器 door_signal <= “01”; – 关门并点亮关门指示灯 door_opened <= ‘0’; – 标记门已关闭 end if; -- 处理紧急制动 if emergency_stop = '1' then moving <= '0'; reached_target_reg <= '0'; door_signal <= "01"; -- 关门 buzz <= '0'; -- 关闭蜂鸣器 door_opened <= '0'; end if; -- 更新请求 requests <= floor_requests; -- 调度逻辑 if moving = '0' then -- 静止时先响应最近楼层 if requests /= "000000" then for i in 0 to 5 loop if requests(i) = '1' then target_floor_reg <= std_logic_vector(to_unsigned(i+1, 4)); if i+1 < to_integer(unsigned(current_floor_reg)) then direction_reg <= '1'; -- 向下 else direction_reg <= '0'; -- 向上 end if; moving <= '1'; exit; end if; end loop; end if; else -- 移动中,优先处理同方向请求 if requests /= "000000" then for i in 0 to 5 loop if requests(i) = '1' then if direction_reg = '0' and i+1 >= to_integer(unsigned(current_floor_reg)) then target_floor_reg <= std_logic_vector(to_unsigned(i+1, 4)); elsif direction_reg = '1' and i+1 <= to_integer(unsigned(current_floor_reg)) then target_floor_reg <= std_logic_vector(to_unsigned(i+1, 4)); end if; exit; end if; end loop; end if; end if; -- 电梯移动逻辑 if moving = '1' then if direction_reg = '0' and to_integer(unsigned(current_floor_reg)) < to_integer(unsigned(target_floor_reg)) then -- 如果向上移动且未到达目标楼层,楼层加1 current_floor_reg <= std_logic_vector(unsigned(current_floor_reg) + 1); buzz <= '1'; -- 移动过程中持续触发蜂鸣器 elsif direction_reg = '1' and to_integer(unsigned(current_floor_reg)) > to_integer(unsigned(target_floor_reg)) then -- 如果向下移动且未到达目标楼层,楼层减1 current_floor_reg <= std_logic_vector(unsigned(current_floor_reg) - 1); buzz <= '1'; -- 移动过程中持续触发蜂鸣器 else -- 如果到达目标楼层,停止移动 moving <= '0'; reached_target_reg <= '1'; -- 设置到达目标楼层标志 buzz <= '0'; -- 停止蜂鸣器 -- 到达目标楼层时立即开门,但仅当门尚未打开时 if door_opened = '0' then buzz <= '1'; -- 打开蜂鸣器 door_signal <= "10"; -- 开门并点亮开门指示灯 door_opened <= '1'; -- 标记门已打开 end if; end if; end if; -- 门的控制逻辑 if moving = '1' then -- 电梯移动时关闭门 if door_signal /= "01" then buzz <= '0'; -- 关闭蜂鸣器 door_signal <= "01"; -- 关门并点亮关门指示灯 door_opened <= '0'; -- 标记门已关闭 end if; end if; end if; end process; -- 统一处理指示灯逻辑 process(clk, reset, direction_reg, door_signal) begin if reset = '1' then -- 复位时初始化指示灯为关门状态 up_indicator <= '0'; down_indicator <= '1'; elsif rising_edge(clk) then -- 根据门状态控制指示灯 case door_signal is when "10" => -- 开门状态 up_indicator <= '1'; -- 开门指示灯 down_indicator <= '0'; when "01" => -- 关门状态 up_indicator <= '0'; down_indicator <= '1'; -- 关门指示灯 when others => up_indicator <= '0'; down_indicator <= '0'; end case; -- 方向控制指示灯 if direction_reg = '0' then up_indicator <= '1'; down_indicator <= '0'; elsif direction_reg = '1' then up_indicator <= '0'; down_indicator <= '1'; end if; end if; end process; -- 将寄存器的值输出到端口 current_floor <= current_floor_reg; target_floor <= target_floor_reg; direction <= direction_reg; reached_target <= reached_target_reg; end Behavioral;帮我为这个代码生成4*4矩阵键盘代码通过current_floor : out STD_LOGIC_VECTOR(3 downto 0); 输入
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值