将五个整数与if,else if语句进行比较

时间:2014-10-24 18:45:43

标签: c++

我需要比较五个整数并找到最大和最小的整数。我需要通过仅使用if和else语句来解决它,我不能使用找到max,min输入的数组或特定函数。

这是我到目前为止所做的:

int n1, n2, n3, n4, n5, biggest,smallest;
n1=biggest;


cout << "Enter the five numbers: ";

cin >> n1 >> n2 >> n3 >> n4 >> n5 ;

if ((n2>=n1) && (n2>=n3) && (n2>=n4) && (n2>=n5)) {
    n2 = biggest;
}
else if ((n3>=n4) && (n3>=n5)) {
    n3 = biggest ;
}
else if (n4>=n5) {
    n4 = biggest ;
}
else {
    n5 = biggest ;
}

cout << biggest ;

为什么输出0而不是最大数字?

6 个答案:

答案 0 :(得分:5)

试试这个:

int main ()
{
int n1, n2, n3, n4, n5, biggest,smallest;
cout << "Enter the five numbers: ";

cin >> n1 >> n2 >> n3 >> n4 >> n5 ;

smallest=biggest=n1;
if(n2>biggest){
    biggest=n2;
}
if(n2<smallest){
      smallest=n2;
}
if(n3>biggest){
   biggest=n3;
}
if(n3<smallest){
      smallest=n3;
}
if(n4>biggest){
    biggest=n4;
}
if(n4<smallest){
      smallest=n4;
}
if(n5>biggest){
     biggest=n5;
}
if(n5<smallest){
      smallest=n5;
}

cout<<smallest<<"\t"<<biggest;

return 0;
}

答案 1 :(得分:0)

如果你必须在所描述的限制下这样做,它将会像(伪代码):

smallest = n1
if n1 <= n2 and n1 <= n3 and n1 <= n4 and n1 <= n5
  smallest = n1
else if n2 <= n3 and n2 <= n4 and...
  smallest = n2
 ...

然后再次反向为最大。纯粹的苦差事。

答案 2 :(得分:0)

这是一种简单的方法。当然有更智能,更快捷的方式,但这个方法有效......

#include <iostream>
using namespace std;

inline void checkBiggestAndSmallest( int& smallest, int& biggest, int n1, int n2, int n3, int n4, int n5 )
{
    if ( n1 >= n2 && n1 >= n3 && n1 >= n4 && n1 >= n5 )
        biggest = n1;

    if ( n1 <= n2 && n1 <= n3 && n1 <= n4 && n1 <= n5 )
        smallest = n1;
}

inline void findBiggestAndSmallest( int& smallest, int& biggest, int n1, int n2, int n3, int n4, int n5 )
{
    checkBiggestAndSmallest( smallest, biggest, n1, n2, n3, n4, n5 );
    checkBiggestAndSmallest( smallest, biggest, n2, n3, n4, n5, n1 );
    checkBiggestAndSmallest( smallest, biggest, n3, n4, n5, n1, n2 );
    checkBiggestAndSmallest( smallest, biggest, n4, n5, n1, n2, n3 );
    checkBiggestAndSmallest( smallest, biggest, n5, n1, n2, n3, n4 );
}

int main()
{
    int n1, n2, n3, n4, n5, biggest = 0, smallest = 0;

    cout << "Enter the five numbers: ";
    cin >> n1 >> n2 >> n3 >> n4 >> n5 ;

    findBiggestAndSmallest( smallest, biggest, n5, n1, n2, n3, n4 );

    cout << "Smallest : " << smallest;
    cout << "Biggest : " << biggest;
}

答案 3 :(得分:0)

假设您有一个变量x(未初始化,并且没有cin值),但是您想将其设置为等于另一个变量,我们将其称为a(未初始化且具有cin值)。

你不能说a = x。

与x = a不同。这就是输出问题的原因。

//assume all variables and libraries are declared

using namespace std;

int main(void)
{
    biggest = n1;

    if (n1 < n2)
        biggest = n2;

    if (n2 < n3)
        biggest = n3;

    if (n3 < n4)
        biggest = n4;

    if (n4 < n5)
        biggest = n5;

    cout << "The biggest input value is " << biggest << ".\n";

请记住,a = x,与x = a不同。

另外一个提示,如果你有一个if语句只适用于它之后的下一行,就像这样:     if(x!= 2)         cout&lt;&lt; “x不等于2.”; 那么你不需要使用{}

将它放在一个块中

但是,如果你有一个多行if语句(或任何其他语句,包括循环) 那么这是必要的,或者程序将执行if语句,在它之后的行,并将其余的行作为常规代码处理,并执行它,即使参数不满足。

像这样:     if(x!= 2)         cout&lt;&lt; “x不等于2.”;         返回0;

只有在上面的情况下才需要块。

我希望有所帮助!

〜垂涎

答案 4 :(得分:0)

您可以使用以下内容:

std::pair<int, int> get_min_max(int n1, int n2)
{
    if (n1 < n2) {
        return {n1, n2};
    } else {
        return {n2, n1};
    }
}

std::pair<int, int> get_min_max(int n1, int n2, int n3, int n4, int n5)
{
    auto p1 = get_min_max(n1, n2);
    auto p2 = get_min_max(n3, n4);
    auto mini = get_min_max(p1.first, p2.first).first;
    auto maxi = get_min_max(p1.second, p2.second).second;
    if (n5 < mini) {
        return {n5, maxi};
    } else if (maxi < n5) {
        return {mini, n5};
    } else {
        return {mini, maxi};
    }
}

Live example

答案 5 :(得分:0)

可变性可以帮助你解决这个问题:

template<typename T>
T Max( T a ) { return a; }

template<typename T,typename ...Ts>
T Max( T a, T b, Ts... ts)
{
    if(a>b) return Max(a,ts...);
    else    return Max(b,ts...);
}

然后你就可以使用它:

biggest = Max( n1, n2, n3, n4, n5 );

Max适用于任意数量的参数(除非之外)

相关问题