Elevator

电梯路径优化算法
本文介绍了一个简单的电梯路径优化问题及其实现代码。该问题关注如何计算电梯完成一系列楼层请求所需的总时间,涉及到电梯向上和向下移动的时间成本以及在每个目标楼层停留的时间。

题目描述
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

题目大致就是说电梯运行过程中,向上一层6秒,向下一层4秒,在指定层数停5秒,有两个注意点就是,起始位置为0层,且要将最后一层所停的时间算在内。

#include<stdio.h>
int main()
{
    int i,j,b,p,n;
    while(scanf("%d",&n)!=EOF)
    {
        int sum=0;
        int a[101];
        if(n==0)
        break;
        else
        {
            a[0]=0;
            for(i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                /*if(a[i]==a[i-1])
                {
                    sum+=5;
                }*/
                if(a[i]>a[i-1])
                {
                    sum+=(a[i]-a[i-1])*6+5;
                }   
                if(a[i]<a[i-1])
                {
                    sum+=(a[i-1]-a[i])*4+5;
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
 } 

以上代码如果没有/* */之间的内容,oj是通不过的,因为没有考虑到,如果下一次指定到达的层数与当层数相同的情况。不过我觉得还真不用考虑,没有人会站在5楼的电梯上,去往5楼,还要为此停5秒。

#include <stdio.h>
int main()
{
    int n;
    while (scanf("%d",&n),n)
    {
        int a,b=0,t,s=0;
        while (n--)
        {
            scanf("%d",&a);
            s+=(t=a-b)>0?t*6+5:-t*4+5;
            b=a;
        }
        printf("%d\n",s);
    }
    return 0;
}

更为简便的方法,用while (scanf(“%d”,&n),n)来结束n为0时的循环,用到了x>y?x:y的语句,在只有两种数据实现的情况下,这种语句是非常简便的。

/* * module_mow.c * * Created on: Feb 19, 2025 * Author: XWM */ #include <module/module_mow/module_mow.h> #include <algorithm/pid/pid.h> #include <driver/driver_analog/driver_analog.h> #include <driver/driver_elevator_motor/driver_elevator_motor.h> #include <driver/driver_grass_motor/driver_grass_motor.h> #include <driver/driver_power/driver_power.h> #include <string.h> #include <stdlib.h> #if MOW_SWITCH /* 割草电机速度pid */ static pid_type mow_grass_pid; static mow_elevator_t mow_elevator; static mow_grass_t mow_grass; /* * 函数名 :mow_init * 功能 :割草组件初始化 * 参数 :无 * 返回值 :无 * 备注 :无 */ void mow_init(void) { memset(&mow_grass_pid, 0, sizeof(pid_type)); memset(&mow_elevator, 0, sizeof(mow_elevator_t)); memset(&mow_grass, 0, sizeof(mow_grass_t)); // mow_elevator.set_gear = MOW_ELEVATOR_GEAR_7; /* 刀盘电机硬件初始化 */ grass_motor_driver_init(); /* 刀盘电机PID初始化 */ pid_init(&mow_grass_pid); pid_param_set(&mow_grass_pid, 0.1, 0.2, 0.01); adc_init(); power_motor_enable(true); } /* * 函数名 :mow_elevator_gear_set * 功能 :设置升降电机档位 * 参数 :gear参考枚举体mow_elevator_gear_e * 返回值 :无 * 备注 :无 */ void mow_elevator_gear_set(mow_elevator_gear_e gear) { mow_elevator.set_gear = gear; /* 同档位重复设置,不运行 */ if(mow_elevator.now_gear == gear) { mow_elevator.status = MOW_ELEVATOR_STATUS_FINISH; } else { mow_elevator.status = MOW_ELEVATOR_STATUS_GO_TO_ORIGIN; } mow_elevator.set_gear = gear; mow_elevator.timer = 0; } /* * 函数名 :mow_grass_speed_set * 功能 :pid 设置割草电机速度 * 参数 :speed速度 单位m/s * 返回值 :无 * 备注 :无 */ void mow_grass_speed_set(float speed) { mow_grass.target_speed = speed; pid_target_set(&mow_grass_pid, speed); } /* * 函数名 :mow_elevator_get * 功能 :割草组件升降电机相关参数获取 * 参数 :无 * 返回值 :参考结构体mow_elevator_get * 备注 :无 */ mow_elevator_t mow_elevator_get(void) { return mow_elevator; } /* * 函数名 :mow_grass_get * 功能 :割草组件刀盘电机相关参数获取 * 参数 :无 * 返回值 :参考结构体mow_grass_t * 备注 :无 */ mow_grass_t mow_grass_get(void) { return mow_grass; } /* * 函数名 :mow_elevator * 功能 :割草升降 * 参数 :无 * 返回值 :无 * 备注 :无 */ static void mow_elevator_execute(void) { if(!ELEVATOR_HALL1_READ()) { if(mow_elevator.press > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.now_gear = MOW_ELEVATOR_GEAR_1; mow_elevator.press = 0; } mow_elevator.release = 0; } else if(mow_elevator.now_gear == MOW_ELEVATOR_GEAR_1) { if(mow_elevator.release > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.now_gear = MOW_ELEVATOR_GEAR_2; mow_elevator.release = 0; } mow_elevator.press = 0; } else if(!ELEVATOR_HALL2_READ()) { if(mow_elevator.press > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.now_gear = MOW_ELEVATOR_GEAR_3; mow_elevator.press = 0; } mow_elevator.release = 0; } else if(mow_elevator.now_gear == MOW_ELEVATOR_GEAR_3) { if(mow_elevator.release > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.now_gear = MOW_ELEVATOR_GEAR_4; mow_elevator.release = 0; } mow_elevator.press = 0; } else if(!ELEVATOR_HALL3_READ()) { if(mow_elevator.press > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.now_gear = MOW_ELEVATOR_GEAR_5; mow_elevator.press = 0; } mow_elevator.release = 0; } else if(mow_elevator.now_gear == MOW_ELEVATOR_GEAR_5) { if(mow_elevator.release > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.now_gear = MOW_ELEVATOR_GEAR_6; mow_elevator.release = 0; } mow_elevator.press = 0; } else if(!ELEVATOR_HALL4_READ()) { if(mow_elevator.press > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.now_gear = MOW_ELEVATOR_GEAR_7; mow_elevator.press = 0; } mow_elevator.release = 0; } else { if(mow_elevator.release > ELEVATOR_HALL_PRESS_RELEASE_TIMER) { mow_elevator.release = 0; } mow_elevator.press = 0; } uint16_t elevator_adc = analog_get().buffer[ADC_ELEVATOR_MOTOR]; mow_elevator.current = elevator_adc; if(elevator_adc < ELEVATOR_LOCKED_ROTOR_ADC) { mow_elevator.adc_timer = 0; } else { /* 堵转10秒 */ if(mow_elevator.adc_timer > ELEVATOR_LOCKED_ROTOR_TIMER) { mow_elevator.status = MOW_ELEVATOR_STATUS_ERROR; } } switch(mow_elevator.status) { case MOW_ELEVATOR_STATUS_GO_TO_ORIGIN: // 机器回到最原点过程 { /* 向下电机反转 */ elevator_motor_control(ELEVATOR_MOTOR_DESCEND); /* 检测是否到达最下点 */ if(mow_elevator.now_gear == MOW_ELEVATOR_GEAR_1) { elevator_motor_control(ELEVATOR_MOTOR_STOP); mow_elevator.status = MOW_ELEVATOR_STATUS_IN_ORIGIN; mow_elevator.timer = 0; } /* 超出时间,报错 */ else if(mow_elevator.timer > ELEVATOR_OVERTIME) { elevator_motor_control(ELEVATOR_MOTOR_STOP); mow_elevator.status = MOW_ELEVATOR_STATUS_ERROR; mow_elevator.timer = 0; } break; } case MOW_ELEVATOR_STATUS_IN_ORIGIN: // 到达原点停上0.5秒 { if(mow_elevator.timer > ELEVATOR_ORIGIN_STOP_TIMER) { mow_elevator.status = MOW_ELEVATOR_STATUS_RUNING; mow_elevator.timer = 0; } break; } case MOW_ELEVATOR_STATUS_RUNING: // 机器到达预定档位过程 { /* 电机开始上升 */ elevator_motor_control(ELEVATOR_MOTOR_RISE); /* 电机运动限定时间 */ if(mow_elevator.timer > ELEVATOR_OVERTIME) { mow_elevator.status = MOW_ELEVATOR_STATUS_ERROR; elevator_motor_control(ELEVATOR_MOTOR_STOP); mow_elevator.timer = 0; } /* 结束动作判断 */ else if(mow_elevator.set_gear == mow_elevator.now_gear) { mow_elevator.status = MOW_ELEVATOR_STATUS_FINISH; elevator_motor_control(ELEVATOR_MOTOR_STOP); mow_elevator.timer = 0; } break; } case MOW_ELEVATOR_STATUS_FINISH: // 达预定档位完毕 { elevator_motor_control(ELEVATOR_MOTOR_STOP); mow_elevator.timer = 0; break; } case MOW_ELEVATOR_STATUS_ERROR: // 故障 { elevator_motor_control(ELEVATOR_MOTOR_STOP); mow_elevator.timer = 0; break; } default: break; } } /* * 函数名 :mow_grass_execute * 功能 :割草刀盘 * 参数 :无 * 返回值 :无 * 备注 :无 */ static void mow_grass_execute(void) { /* 每毫秒获取编码器脉冲,转化为速度r/s */ float speed = (mow_motor_encode_get() * 20) / GRASS_CIRCLE_PULSE; if(GRASS_MOTOR_FR_READ() == GPIO_PIN_SET) { speed = -speed; } mow_motor_encode_set(0); mow_grass.now_speed = speed; mow_grass.status = speed != 0 ? MOW_GRASS_STATUS_WORKING : MOW_GRASS_STATUS_STOP; mow_grass.current = analog_get().buffer[ADC_GRASS_MOTOR]; int16_t pwm_grass = pid_realize(&mow_grass_pid, mow_grass.now_speed, GRASS_DEAD_ZONE, GRASS_INTEGRAL_ERR, GRASS_INTEGRAL_LIMIT); if(abs(pwm_grass) <= GRASS_MIN_PWM) { pwm_grass = 0; } mow_motor_pwm(pwm_grass); mow_grass.brake = GRASS_MOTOR_BRAKE_READ(); } /* * 函数名 :mow_timer_50ms_execute * 功能 :电机闭环定时器执行 * 参数 :无 * 返回值 :无 * 备注 :无 */ void mow_timer_50ms_execute(void) { adc_convert(); /* 割草升降 */ mow_elevator_execute(); /* 割草刀盘 */ mow_grass_execute(); } /* * 函数名 :mow_timer_1ms_execute * 功能 :割草组件计时 * 参数 :无 * 返回值 :无 * 备注 :无 */ void mow_timer_1ms_execute(void) { mow_elevator.press++; mow_elevator.release++; mow_elevator.adc_timer++; mow_elevator.timer++; } #endif 给上述添加注释
08-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值