STD 1009 Elevator

Elevator

Time Limit: 1000ms   Memory limit: 32768K  有疑问?点这里^_^

题目描述

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.  

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

输入

There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

输出

Print the total time on a single line for each test case.

示例输入

1 2
3 2 3 1
0

示例输出

17
41

提示


来源

 

示例程序

 
#include<stdio.h>  
int main()  
{  
    int i,j,n,m,a[100],b,c;  
    while(scanf("%d",&n)&&n)  
    {  
        m=0;  
        b=0;  
        c=0;  
        for(i=0;i<n;i++)  
        {  
            scanf("%d",&a[i]);  
            if(a[i]-b>0)  
                c+=(a[i]-b)*6;  
            else  
                c+=(b-a[i])*4;  
            c+=5;  
            b=a[i];  
        }  
        printf("%d\n",c);  
    }  
}  


library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.NUMERIC_STD.ALL; entity elevator_controller is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC; -- 键盘输入 keypad_row : in STD_LOGIC_VECTOR(3 downto 0); keypad_col : out STD_LOGIC_VECTOR(3 downto 0); key_value : out STD_LOGIC_VECTOR(3 downto 0); key_valid : buffer STD_LOGIC; -- 红外信号 ir_in : in STD_LOGIC; ir_out : out STD_LOGIC; -- 电梯控制 floor_req : in STD_LOGIC_VECTOR(3 downto 0); current_floor : out STD_LOGIC_VECTOR(2 downto 0); door_open : out STD_LOGIC; door_close : out STD_LOGIC; motor_up : out STD_LOGIC; motor_down : out STD_LOGIC ); end elevator_controller; architecture Behavioral of elevator_controller is -- 键盘扫描模块信号 signal debounce_counter : integer range 0 to 100000 := 0; signal col_index : integer range 0 to 3 := 0; signal key_pressed : STD_LOGIC := '0'; signal prev_row : STD_LOGIC_VECTOR(3 downto 0) := "1111"; -- 红外模块信号 signal ir_buffer : STD_LOGIC_VECTOR(7 downto 0) := (others => '0'); signal bit_counter : integer range 0 to 7 := 0; signal receive_active : STD_LOGIC := '0'; -- 电梯控制信号 type state_type is (IDLE, MOVING_UP, MOVING_DOWN, DOOR_OPENING, DOOR_CLOSING); signal current_state, next_state : state_type; signal current_floor_reg : unsigned(2 downto 0) := "000"; signal target_floor : unsigned(2 downto 0) := "000"; signal keypad_value : STD_LOGIC_VECTOR(3 downto 0); begin -- 键盘扫描进程 (整合原keypad_scanner模块) process(clk, reset) begin if reset = '1' then keypad_col <= "1111"; keypad_value <= "0000"; key_valid <= '0'; debounce_counter <= 0; col_index <= 0; key_pressed <= '0'; prev_row <= "1111"; elsif rising_edge(clk) then -- 列扫描 case col_index is when 0 => keypad_col <= "1110"; when 1 => keypad_col <= "1101"; when 2 => keypad_col <= "1011"; when 3 => keypad_col <= "0111"; when others => keypad_col <= "1111"; end case; -- 按键检测 if keypad_row /= "1111" then if prev_row = "1111" then key_pressed <= '1'; debounce_counter <= 0; end if; else key_pressed <= '0'; key_valid <= '0'; end if; -- 消抖处理 if key_pressed = '1' then if debounce_counter < 100000 then debounce_counter <= debounce_counter + 1; else key_valid <= '1'; -- 按键解码 case col_index is when 0 => if keypad_row = "0111" then keypad_value <= "0001"; else if keypad_row = "1011" then keypad_value <= "0100"; else if keypad_row = "1101" then keypad_value <= "0111"; else if keypad_row = "1110" then keypad_value <= "0000"; else key_valid <= '0'; end if; end if; end if; end if; when 1 => if keypad_row = "0111" then keypad_value <= "0010"; else if keypad_row = "1011" then keypad_value <= "0101"; else if keypad_row = "1101" then keypad_value <= "1000"; else if keypad_row = "1110" then keypad_value <= "1111"; else key_valid <= '0'; end if; end if; end if; end if; when 2 => if keypad_row = "0111" then keypad_value <= "0011"; else if keypad_row = "1011" then keypad_value <= "0110"; else if keypad_row = "1101" then keypad_value <= "1001"; else if keypad_row = "1110" then keypad_value <= "1110"; else key_valid <= '0'; end if; end if; end if; end if; when 3 => if keypad_row = "0111" then keypad_value <= "1010"; else if keypad_row = "1011" then keypad_value <= "1011"; else if keypad_row = "1101" then keypad_value <= "1100"; else if keypad_row = "1110" then keypad_value <= "1101"; else key_valid <= '0'; end if; end if; end if; end if; when others => key_valid <= '0'; end case; end if; end if; prev_row <= keypad_row; if col_index < 3 then col_index <= col_index + 1; else col_index <= 0; end if; end if; end process; -- 红外模块 (整合原ir_transceiver模块) process(clk, reset) variable carrier_counter : integer range 0 to 1315 := 0; begin if reset = '1' then ir_out <= '0'; receive_active <= '0'; bit_counter <= 0; ir_buffer <= (others => '0'); elsif rising_edge(clk) then -- 红外接收 if ir_in = '0' then if receive_active = '0' then receive_active <= '1'; bit_counter <= 0; end if; else if receive_active = '1' then if debounce_counter mod 1000 = 0 then ir_buffer(bit_counter) <= ir_in; if bit_counter < 7 then bit_counter <= bit_counter + 1; else receive_active <= '0'; end if; end if; end if; end if; -- 红外发射 if receive_active = '1' then if carrier_counter < 658 then ir_out <= '1'; else ir_out <= '0'; end if; if carrier_counter = 1315 then carrier_counter := 0; else carrier_counter := carrier_counter + 1; end if; else ir_out <= '0'; end if; end if; end process; -- 电梯状态机 (保持原有逻辑) process(clk, reset) begin if reset = '1' then current_state <= IDLE; current_floor_reg <= "000"; elsif rising_edge(clk) then current_state <= next_state; case current_state is when MOVING_UP => current_floor_reg <= current_floor_reg + 1; when MOVING_DOWN => current_floor_reg <= current_floor_reg - 1; when others => null; end case; end if; end process; process(current_state, current_floor_reg, target_floor, floor_req, keypad_value) begin next_state <= current_state; door_open <= '0'; door_close <= '0'; motor_up <= '0'; motor_down <= '0'; case current_state is when IDLE => if target_floor > current_floor_reg then next_state <= MOVING_UP; else if target_floor < current_floor_reg then next_state <= MOVING_DOWN; else next_state <= DOOR_OPENING; end if; end if; when MOVING_UP => motor_up <= '1'; if current_floor_reg = target_floor then next_state <= DOOR_OPENING; end if; when MOVING_DOWN => motor_down <= '1'; if current_floor_reg = target_floor then next_state <= DOOR_OPENING; end if; when DOOR_OPENING => door_open <= '1'; next_state <= DOOR_CLOSING; when DOOR_CLOSING => door_close <= '1'; next_state <= IDLE; when others => next_state <= IDLE; end case; end process; current_floor <= STD_LOGIC_VECTOR(current_floor_reg); key_value <= keypad_value; -- 楼层请求处理 process(keypad_value) begin if key_valid = '1' then case keypad_value is when "0001" => target_floor <= "000"; -- 1楼 when "0010" => target_floor <= "001"; -- 2楼 when "0011" => target_floor <= "010"; -- 3楼 when "0100" => target_floor <= "011"; -- 4楼 when "0101" => target_floor <= "100"; -- 5楼 when "0110" => target_floor <= "101"; -- 6楼 when others => null; end case; end if; end process; end Behavioral;批注
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值