首页 > 解决方案 > 获取具有特定距离的键值对数组中的第二个最大数

问题描述

所以,问题是:

您如何获得点[位置] [阻力] 中的第二高阻力,与最高阻力的位置差异至少为 2-10?

C、C++、Arduino 或伪代码,我真的不介意。


这是我的代码:

/* ###### VARIABLE ######## */
 /*  */
found bool = false; /* while our point is not found, FOUND IS FALSE */
resistance int []; /* resistance vector */
location int []; /* Motor location vector */
current_location int = 0; /* 0 which means the top part of the motor container */
current_resistance int; /* the resistance which the Hall Sensor registers at that moment */
points HashMap; /* HashMap with location as key and resistance at its location as value */
min_distance int = 50; /* value for the minimum distante between the highest point and the second highest point (Variable to be modified as we test more) */

/* ######## FUNCTIONS ######### */
 /*  */
move_check()
{
    location.push(current_location);
    current_resistance = GET_CURRENT_RESISTANCE;
    resistance.push(current_resistance);
    MOVE_MOTOR++;
    location++;
}

map_points()
{
    for(;location <= MAX_DISTANCE;) /* WE SET THE MAX_DISTANCE TO WHAT WE MEASURED BEFORE AS MAX DISTANCE */
    {
        move_check();
        RED_LED = 1;
        RED_LED = 0; /* TO SEE THAT IS MAPPING POINTS */
        }
    }
    CreateHashMap (points, location, resistance, MAX_DISTANCE); /* (VARIABLE_NAME , int, int, NUMBER_OF_ITEMS) */
}

get_top() /* gets the thryoid Cartilage location */
{
    maximum int = 0;
    max_location int = 0;
    for(int i = 0; i<= location.length(); i++)
    {
        if(maximum > max(points[i], maximum))
        {
            maximum = max(points[i], maximum);
        }
        else
        {
            maximum = max(points[i], maximum);
            max_location = i;
        }
    }
    return max_location;
}

get_bottom() /* gets the cricoid cartilage location*/
{
    bottom_point int;
    ??????????????????????????????
    return bottom_point;
}

goto_middle_point()
{
    RED_LED = 1;
    while(location != floor((get_top() + get_bottom()) / 2))
    {
        location--;
    }
    RED_LED = 0;
    BEEP_SPEAKER_LONG;
}

wait_button_press()
{
    resistance_now int = GET_CURRENT_RESISTANCE;
    while(resistance_now != GET_CURRENT_RESISTANCE)
    {
        RED_LED = 1;
        RED_LED = 0;
    }
    SLEEP(1000);
    found = true;
}

done(bool found)
{
    if(found)
    {
        while(location)/*  if location is 0 == false (top location) */
        {
            location--;
            MOVE_MOTOR--;
        }
    }
    BEEP_SPEAKER;
    BEEP_SPEAKER; /* beep speaker twice for done */
    RED_LED = 0; /* stops working */
    GREEN_LED = 1; /* signal for ready */
}

int main()
{
    map_points();
    goto_middle_point();
    wait_button_press();
    done();
    return 0;
}

标签: c++carduinopseudocode

解决方案


我不知道您正在使用哪种 Arduino HashMap 实现,但如果是这个,这是我能想到的最好的 API。它肯定不是最优的,更好的数据结构会更快。

假设我正确理解了这个问题,我会做一个线性搜索。你知道如何获得最高点。您知道第二高点的位置必须低于最高点。(我在这里添加,如果您的坐标系不同,您可能需要减去)。假设位置的最小差异必须是min_dist(某个值 2-10)。

HashMap<int, int, N> points = ...;
int key_low = upper_position + min_dist;
int peak_location = -1;
for(int i=0; i < points.size(); ++i)
{ 
    int location = points.keyAt(i);
    if (location < key_low) continue;
    int resistance = points[location];
    if (peak_location < 0 || resistance > points[peak_location])
    {
        peak_location = location; 
    }
}

推荐阅读