3

我正在尝试使用变量中的值作为上限来获取数组中最近的上限(除非找到等效值)的索引,然后在数组中的同一索引处找到值。sizesumvalue

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

4 回答 4

1

将您的代码更改为此 -

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

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

于 2014-07-23T09:10:23.953 回答
1

要获得上限的索引,只需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;
于 2014-07-23T09:15:51.670 回答
0

最简单的方法可以在 2 行中完成:

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必须对 sum 进行分区。如您的示例中的排序数组符合此条件。

于 2014-07-23T09:16:38.283 回答
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;
}

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

于 2019-10-07T01:28:02.090 回答