搜索数组的上限

时间:2014-07-23 08:39:07

标签: c++ arrays upperbound

我尝试使用变量size中的值作为数组sum中最近上限的索引(除非找到等效值)上限,然后在数组value中的相同索引处查找值。

例如:如果sum中的值为270,我的程序应该在size中找到位于索引6处的值280,并输出相应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;
}

4 个答案:

答案 0 :(得分:0)

将您的代码更改为 -

    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";
        }
    }

还有其他方法可以解决这个问题。但正如你所说,你是初学者。我给出了简单的强力解决方案。 :)

答案 1 :(得分:0)

要获取上限的索引,只需使用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);

然后使用index,例如为:

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

答案 2 :(得分:0)

最简单的方法可以分为两行:

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;

请注意,必须对size进行分区。您的示例中的排序数组符合此条件。

答案 3 :(得分:0)

我认为最好以此来获得上限的索引:

upper_bound(size, size+27, sum) - size

获取上限值:

int index = upper_bound(size, size+27, sum) - size;
cout << size[index] << endl;

我使用以下代码测试了性能。 for循环下的每个单行代码都给出了上限的索引。

#include <bits/stdc++.h>
using namespace std;

int main(){
    unsigned char Array[8] = {5, 10, 15, 20, 25, 30, 35, 40};
    unsigned char* it = upper_bound(Array, Array+8, 23);
    for(int i = 0 ;i < 100000000; ++i)//0.46s
        distance(Array, it);
    for(int i = 0 ;i < 100000000; ++i)//0.21s
        it - begin(Array);
    for(int i = 0 ;i < 100000000; ++i)//0.19s
        it - Array;
}

在我的测试中,最后一个是最快的。