简体   繁体   English

搜索数组的上限

[英]Searching for the upper bound in an array

I'm trying to get the index of the nearest upper bound (unless equivalent value found) in the array size using the value in variable sum as upper bound, and then find the value at the same index in the array value . 我正在尝试使用变量sum的值作为上限来获取数组size最近上限的索引 (除非找到等效值),然后在数组value的相同索引处找到该value

For example: if the value in sum is 270, my program should find the value 280 located at index 6 in size and output the value at corresponding value[6] . 例如:如果在值sum是270,我的节目应该找到在位于索引6的值280 size和输出在对应的值value[6]

#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

int main()
{
    double x = 0;
    double y = 0;
    double sum = 0;
    double size[27] = {24, 28, 32, 38, 48, 240, 280, 320, 360, 380,
                       420, 480, 560, 600, 640, 700, 720, 800, 840,
                       960, 980, 1120, 1200, 1280, 1440, 1680, 1920};

    double value[27] = {.0022, .0026, .0029, .0035, .0044, .0219,
                        .0256, .0292, .0328, .0384, .0438, .0513,
                        .0547, .0584, .0641,.0656, .073, .0766,
                        .0875, .0877, .0897, .1023, .1094, .1169,
                        .1313, .1531, .175};

    cout << "Enter width: " << endl;
    cin >> x;
    cout << "Enter height: " << endl;
    cin >> y;

    x = ceil(x) + 3;
    y = ceil(y) + 3;

    sum = x * y;
}

Change your code to this - 将您的代码更改为此 -

    double x = 0;
    double y = 0;
    double sum = 0;
    int size[27] = {24, 28, 32, 38, 48, 240, 280, 320, 360, 380,
    420, 480, 560, 600, 640, 700, 720, 800, 840, 960, 980, 1120, 1200, 1280, 1440, 1680, 1920};
    double value[27] = {.0022, .0026, .0029, .0035, .0044, .0219,
    .0256, .0292, .0328, .0384, .0438, .0513, .0547, .0584, .0641,.0656, .073, .0766, .0875, .0877, .0897, .1023, .1094, .1169, .1313, .1531, .175};

    cout << "Enter width: " << endl;
    cin >> x;
    cout << "Enter height: " << endl;
    cin >> y;

    x = ceil(x) + 3;
    y = ceil(y) + 3;

    sum = x * y;

    for (int i=0;i<27;i++)
    {
        if (size[i]>=sum)
        {
          cout<<value[i]<<endl; 
          break;
        }
        else if(i==26)
        {
            cout<<"No upper Bound find\n";
        }
    }

There are other ways to solve this. 还有其他方法可以解决这个问题。 But as you said you are a beginner. 但正如你所说,你是初学者。 I have given the simple bruteforce solution. 我给出了简单的强力解决方案。 :) :)

To get the index of the upper bound simply use std::upper_bound like this (requires that the range is at least partially sorted): 要获取上限的索引,只需像这样使用std::upper_bound (要求范围至少部分排序):

// Get iterator to upper bound.
auto it = std::upper_bound(std::begin(size), std::end(size), sum);

// Get index by iterator subtraction.
std::size_t index = it - std::begin(size);

Then use index eg as: 然后使用index例如:

std::cout << value[index] << std::endl;

The simplest way can be done in 2 lines: 最简单的方法可以分为两行:

auto size_ub = std::upper_bound(std::begin(size), std::end(size), sum);
int idx = std::distance(std::begin(size), size_ub);

cout << value[idx] << endl;

Please note that size must be partitioned with respect to sum. 请注意, size必须按总和进行分区。 A sorted array as in your example meets this criteria. 您的示例中的排序数组符合此条件。

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

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