寻找最小的数字,该数字不为零

时间:2019-10-31 11:57:13

标签: c++ math

我有三个称为a,b,c的整数,需要找到它们的最小数,即0。 但是同时,它应该能够处理所有三个数字均为0的特殊情况。

必须使用C或C ++ 14来实现

示例1:

a = 4;
b = 0;
c = 1;

Result = c

示例2

a = 0;
b = 0;
c = 0;

Result = Special case

示例3

a = 11;
b = 46;
c = 15;

Result = a

到目前为止,我还没有找到一种优雅的方法来用c或c ++来实现。

我已经考虑过将其放在一个有序数组中,并不断弹出数字,直到得到除零以外的其他值。但这似乎是一种过于复杂的方法。

修改

忘了提及我尝试找到的值从不为负,而是始终为正。

已删除代码,因为它不应该是代码审查

5 个答案:

答案 0 :(得分:2)

如果只有三个值,可以使用简单的if语句来解决。

那么逻辑可能是(伪代码

if (a is larger than zero) and (b is larger than zero and a is smaller than b) and (c is larger than zero and a is smaller than c)
    a is the smallest
else if (b is larger than zero) and (c is larger than zero and b is smaller than c)
    b is the smallest
else if c is larger than zero
    c is the smallest
else
    all are either zero or negative

请注意,每个if支票都会逐渐变小。这是因为先前的条件删除了不需要进一步检查的替代项。

这当然适用于较大的变量链,只要变量数量固定即可。不过,它很快就会变得笨拙,因此对于超过三到四个变量,应该使用其他方法。

答案 1 :(得分:1)

大部分控制流程如下:

#include <iostream>


int find_smallest_nonzero(int a, int b, int c)
{
    if (a > 0 || b > 0 || c > 0) {
       if ((b >= a || b == 0) && (c >= a || c == 0) && (a > 0))
           return a;
       else if ((c >= b || c == 0) && (b != 0))
           return b;
       else
           return c;
    } else return -1;
}

int main() {
    std::cout 
            // permutations
            << find_smallest_nonzero(1, 2, 3) << ' '
            << find_smallest_nonzero(2, 1, 3) << ' '
            << find_smallest_nonzero(2, 3, 1) << ' '

            // one zeros
            << find_smallest_nonzero(1, 0, 3) << ' '
            << find_smallest_nonzero(0, 1, 3) << ' '
            << find_smallest_nonzero(1, 3, 0) << ' '

            // two zeros
            << find_smallest_nonzero(1, 0, 0) << ' '
            << find_smallest_nonzero(0, 1, 0) << ' '
            << find_smallest_nonzero(0, 0, 1) << ' '

            // duplicates
            << find_smallest_nonzero(2, 2, 1) << ' '
            << find_smallest_nonzero(2, 1, 2) << ' '
            << find_smallest_nonzero(1, 2, 2) << ' '

            << find_smallest_nonzero(1, 1, 2) << ' '
            << find_smallest_nonzero(1, 2, 1) << ' '
            << find_smallest_nonzero(2, 1, 1) << ' '

            // all zeros
            << find_smallest_nonzero(0, 0, 0) << '\n';
}
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1

它将返回abc中的最小非零值,除非所有均为0,在这种情况下将返回-1。 / p>

可能会有更快的方法。

答案 2 :(得分:0)

int lowest(int a, int b, int c) {
    auto v = {a, b, c};
    return *std::min_element(std::begin(v), std::end(v), [](int x, int y) {
        return (x < y && x != 0) || y == 0;
    });
}

此方法可以处理任意数量的元素。在特殊情况下,该函数仅返回零(无需使用其他特殊值)。

答案 3 :(得分:0)

您可以使用std::min

在C ++中执行以下操作
int a = 0,b = 0,c = 0;
int x = std::min( { a,b,c },
        [](const auto& n1, const auto& n2) {
             if (not n1)
               return false;
             if (not n2)
               return true;
             return n1 < n2;
         });
std::string out = x?std::to_string(x):std::string{"Special Case"};

答案 4 :(得分:-1)

if(a==0 && b==0 && c==0){
    return "Special case"
}else{
    // now put each equals zero and compare the other, last else compare all three
}

您可以在输出中写一个函数printf。

相关问题