C++ 中给定值的第一个最小值

发布于 2024-12-22 12:58:31 字数 449 浏览 1 评论 0原文

    string result;

    double zpls, zmin, ypls, ymin, xpls, zmin ;

    zpls = 4.2;
    zmin = 0;
    ypls = -2;
    ymin = 4.2;
    xpls = 6;
    xmin = 8;

如何在这些值中找到“第一个最小正数”并设置

    result = "+Z";// or wichever value is lowest -z, +Y etc  

(在C++中)

我尝试了数组,如果其他和 min(zpls,min(zmin,min(....))) // 使用 #include

但无法正确获取,

谢谢

    string result;

    double zpls, zmin, ypls, ymin, xpls, zmin ;

    zpls = 4.2;
    zmin = 0;
    ypls = -2;
    ymin = 4.2;
    xpls = 6;
    xmin = 8;

How to find "first minimum positive" among these values and set

    result = "+Z";// or wichever value is lowest -z, +Y etc  

(in C++)

i tried arrays , if else and
min(zpls,min(zmin,min(....))) // with #include <algorithm>

but could not get it correctly

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左岸枫 2024-12-29 12:58:31

只要您拥有自变量中的数据,您就需要

result = ""; // no result yet
double minValue = std::numeric_limits<double>::max( );
if( zplus > 0 && zpls < minValue ) {
  minValue = zpls;
  result = "+Z"
}
if( zmin > 0 && zmin < minValue ) {
  minValue = zmin;
  result = "-Z"
}

按照该模式执行 if-else 等操作。最后的 result 要么包含您想要的字符串,要么为空(在这种情况下,没有正值。)

这是一个完整的示例:

#include <iostream>
#include <limits>
#include <string>

int main () {
    std::string result;

    double zpls, zmin, ypls, ymin, xpls, xmin ;

    zpls = 4.2;
    zmin = 0;
    ypls = -2;
    ymin = 4.2;
    xpls = 6;
    xmin = 8;

    result = ""; // no result yet
    double minValue = std::numeric_limits<double>::max( );
    if( zpls > 0 && zpls < minValue ) {
      minValue = zpls;
      result = "+Z";
    }
    if( zmin > 0 && zmin < minValue ) {
      minValue = zmin;
      result = "-Z";
    }
    if( ypls > 0 && ypls < minValue ) {
      minValue = ypls;
      result = "+Y";
    }
    if( ymin > 0 && ymin < minValue ) {
      minValue = ymin;
      result = "-Y";
    }

    if( xpls > 0 && xpls < minValue ) {
      minValue = xpls;
      result = "+X";
    }
    if( xmin > 0 && xmin < minValue ) {
      minValue = xmin;
      result = "-X";
    }


    std::cout << "The first mininum positive element is: " << result << "\n";
}

As long as you have the data in independent variables, you'll need to do an if-else

result = ""; // no result yet
double minValue = std::numeric_limits<double>::max( );
if( zplus > 0 && zpls < minValue ) {
  minValue = zpls;
  result = "+Z"
}
if( zmin > 0 && zmin < minValue ) {
  minValue = zmin;
  result = "-Z"
}

and so forth, in that pattern. At the end result will either have the string you want, or it will be empty (in which case, there were no positive values.)

Here is a complete sample:

#include <iostream>
#include <limits>
#include <string>

int main () {
    std::string result;

    double zpls, zmin, ypls, ymin, xpls, xmin ;

    zpls = 4.2;
    zmin = 0;
    ypls = -2;
    ymin = 4.2;
    xpls = 6;
    xmin = 8;

    result = ""; // no result yet
    double minValue = std::numeric_limits<double>::max( );
    if( zpls > 0 && zpls < minValue ) {
      minValue = zpls;
      result = "+Z";
    }
    if( zmin > 0 && zmin < minValue ) {
      minValue = zmin;
      result = "-Z";
    }
    if( ypls > 0 && ypls < minValue ) {
      minValue = ypls;
      result = "+Y";
    }
    if( ymin > 0 && ymin < minValue ) {
      minValue = ymin;
      result = "-Y";
    }

    if( xpls > 0 && xpls < minValue ) {
      minValue = xpls;
      result = "+X";
    }
    if( xmin > 0 && xmin < minValue ) {
      minValue = xmin;
      result = "-X";
    }


    std::cout << "The first mininum positive element is: " << result << "\n";
}
美羊羊 2024-12-29 12:58:31

如果您在任何集合上运行 std::sort数字,“第一个最小正值”将只是“第一个正值”。

如果您在该排序集合上运行 std::find_if ,你可以找到第一个正数。

if you run std::sort on any collection of numbers, the "first minimum positive" will simply be the "first positive".

If you run std::find_if on that sorted collection, you can find that first positive.

找个人就嫁了吧 2024-12-29 12:58:31

真的需要单独的变量吗?为什么不这样设计:

enum Tags
{
 ZPlus,
 ZMinus,
 Etc
};

const std::string [] names = {"z+", "z-", "etc" }

int values[3];

并插入 zmin = -3; 你会得到 values[ZMinus] = -3;
然后你可以在 values 数组中找到最小索引并返回名称[minimumIndex];

are you really need sepearte variables. Why not to design like this:

enum Tags
{
 ZPlus,
 ZMinus,
 Etc
};

const std::string [] names = {"z+", "z-", "etc" }

int values[3];

and insted of zmin = -3; you get values[ZMinus] = -3;
then you can find minimum index in values array and return names[minimumIndex];

桃气十足 2024-12-29 12:58:31

不要调整算法。该算法很清晰(“找到最小值”)。相反,调整搜索条件,并保持在 O(n) 范围内。

代码。

#include <algorithm>
#include <vector>
#include <iostream>
int main () {
    // std::min_element()
    std::vector<float> vec;

    vec.push_back(0);
    vec.push_back(-1);
    vec.push_back(-2);
    vec.push_back(2);
    vec.push_back(4);

    auto cmp = [](float lhs, float rhs) {
        const bool lz = lhs < 0,
                   rz = rhs < 0;
        if (lz && rz) return lhs < rhs;
        if (lz) return false;
        if (rz) return true;
        return lhs < rhs;
    };

    const float mp = *std::min_element (vec.begin(), vec.end(), cmp);        
    std::cout << mp << '\n';

    // demonstration of our comparison
    sort (vec.begin(), vec.end(), cmp);
    for (auto it=vec.begin(), end=vec.end(); it!=end; ++it)
        std::cout << *it << " ";
    std::cout << std::endl;
}

输出。

0
0 2 4 -1 -2

解释。

我们的排序函数是用 cmp 编码的。它检查其操作数的符号。如果两者均为负数,则较大者获胜。如果只有 LHS 为负,则在排序时自动优先选择 RHS。相反,如果 RHS 为负,则首选 LHS。两者都是积极的,我们回到正常秩序。

好的一点是,它在范围内运行一次,时间复杂度为 O(n)。

Don't adjust algorithms. The algorithm is clear ("find minimum"). Adjust the search criteria instead, and stay in O(n) realm.

Code.

#include <algorithm>
#include <vector>
#include <iostream>
int main () {
    // std::min_element()
    std::vector<float> vec;

    vec.push_back(0);
    vec.push_back(-1);
    vec.push_back(-2);
    vec.push_back(2);
    vec.push_back(4);

    auto cmp = [](float lhs, float rhs) {
        const bool lz = lhs < 0,
                   rz = rhs < 0;
        if (lz && rz) return lhs < rhs;
        if (lz) return false;
        if (rz) return true;
        return lhs < rhs;
    };

    const float mp = *std::min_element (vec.begin(), vec.end(), cmp);        
    std::cout << mp << '\n';

    // demonstration of our comparison
    sort (vec.begin(), vec.end(), cmp);
    for (auto it=vec.begin(), end=vec.end(); it!=end; ++it)
        std::cout << *it << " ";
    std::cout << std::endl;
}

Output.

0
0 2 4 -1 -2

Explanation.

Our sorting function is encoded in cmp. It checks the signs of its operands. If both are negative, the bigger one wins. If only the LHS is negative, then RHS is automatically preferred in the sorting. On the opposite, if the RHS is negative, LHS is preferred. Of both are positive, we fall back to the normal order.

Nice thing is that this runs exactly one time over the range and in O(n).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文