Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        4年前关闭。
                                                                                            
                
        
为了简化我的问题,我将使用像素作为模板。



计算像素的最高值,中值和最低值的最快方法是什么?
例如:

unsigned char pixel[] = {128, 255, 64};
unsigned char highest, median, lowest;

get_pixel_values_priority(pixel, &highest, &median, &lowest);
printf("Highest %u\nMedian %u\nLowest %u", highest, median, lowest);


应导致:

Highest 255
Median 128
Lowest 64

最佳答案

由于您使用C ++对此进行了标记,因此我将结合使用std::vector对象和std::sort()算法来轻松查找这些统计信息。这是一个简单的例子。

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<float> numbers = { 1, 4, 3, 2 };

    std::sort(numbers.begin(), numbers.end());

    // numbers = { 1, 2, 3, 4 }

    float min = numbers.front();
    float max = numbers.back();

    float median;
    if (numbers.size() % 2) {   // odd number of elements
        median = numbers[numbers.size() / 2];
    }
    else {                      // even number of elements
        median = (numbers[numbers.size() / 2] + numbers[numbers.size() / 2 - 1]) / 2;
    }
}

08-16 21:45