简体   繁体   中英

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.

Or, if the given numbers are 1, 325, 350, 299 the average will be 243.75 so the closest number is 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 )

#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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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