简体   繁体   English

我需要找到与给定数字中的平均值最接近(和次要)的数字。

[英]I need to find the closest (and minor) number to the average in the given numbers.

I need to find the closest (and minor) number to the average in the given numbers. 我需要找到与给定数字中的平均值最接近(和次要)的数字。 For example: 例如:

If the given numbers are 1,2,3,4,5 the average will be 3, and the closest numbers are 2 and 4 but the minor is 2 so the result should be 2. 如果给定数字为1,2,3,4,5,则平均值将为3,最接近的数字为2和4,但次要数字为2,因此结果应为2。

Or, if the given numbers are 1, 325, 350, 299 the average will be 243.75 so the closest number is 299. 或者,如果给定的数字为1、325、350、299,则平均值为243.75,因此最接近的数字为299。

I only managed to find the average. 我只设法找到平均值。

#include <iostream>
using namespace std;
int main() {
    int n, i;
    float num[100], sum=0.0, average;
    n = 10;
    while (n > 100 || n <= 0) {
           cin >> n;}
    for(i = 0; i < n; ++i) {
        cin >> num[i];
        sum += num[i];
        } average = sum / n;
    cout << average;
    return 0;
    }

Once you've found the average, you can iterate over the array again and find the closest number: 找到平均值后,您可以再次遍历数组并找到最接近的数字:

float closest = num[0];
float diff = abs(average - closest);
for(i = 1; i < n; ++i) {
    float curr_diff = abs(average - num[i]);
    if (curr_diff < diff || (curr_diff == diff && num[i] < closest) {
        closest = num[i];
        diff = curr_diff;
    }
}

Here is a solution using algorithm functions: ( std::upper_bound , std::sort ) and a function from the <numeric> library, std::accumulate ) 这是使用算法函数的解决方案:( std :: upper_boundstd :: sort )和<numeric>库中的函数std :: accumulate

#include <algorithm>
#include <numeric>
#include <iostream>
#include <cmath>

int main()
{
    float values[] = { 1, 2, 3, 4, 5 };
    const int numValues = sizeof(values) / sizeof(float);

    // get the average
    float average = std::accumulate(values, values + numValues, 0.0F) / numValues;

    // sort the values
    std::sort(values, values + numValues);

    // find where the average would be inserted
    float *closest = std::upper_bound(values, values + numValues, average);

    // assume closest is the value greater than average
    float *absClosest = closest;

    // check number before the insertion point
    if (closest != values)
    {
        float *closest2 = closest - 1;

        // get the difference in both numbers and average
        if (fabs(*closest2 - average) < fabs(*closest - average))
            absClosest = closest2;
    }
    std::cout << "The average is " << average << "\nThe closest to average is " << *absClosest;
}

Live Example 现场例子

You will have to check for the closest, the smaller value and not equal to the average. 您将需要检查最接近的,较小的值并且不等于平均值​​。 It could therefore be something like: 因此,它可能类似于:

#include <cstdio>
#include <math.h>

using namespace std;

int main( int argc, char ** argv )
{
int n = 5;
float num[] = {1,2,3,4,5};
float average = 3;
float smallValue = 0.0001;

float closest = num[0];
float diff = fabs(average-closest);

for (int i = 1; i < n; ++i) {
    float curr_diff = fabs(average - num[i]) ;
    if ((curr_diff + smallValue) < diff && fabs(num[i] - average) > smallValue)
    {
        closest = num[i];
        diff = curr_diff;
    }
}
return 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM