最小的3个整数,并在集团中以最小的数字打印

时间:2014-05-21 02:00:31

标签: c++ cout

我用C ++编写了我的第一个程序,它读入三个整数并确定哪个是该组中最小的数字。但是,我需要一些关于如何提示用户并使用cout打印消息从控制台读取三个值的指导。

在我的伪代码中,我认为它是......"" a, b, and c is x中的最小值(ab,{{1} },c被实际值替换。)

我只是坚持如何使用我的代码实现它。

我的要求是:

  • 比较运算符,x&&
  • 允许其他和嵌套

非常感谢任何指导

||

2 个答案:

答案 0 :(得分:3)

输入后输入是正确的:

if(a<b){
  if(a<c){
    cout << "the smallest of the numbers is: " <<a<<endl;
  }
  else{
    cout << "the smallest of the numbers is: " <<c<<endl;
  }
}
else {
 if(b<c){
    cout << "the smallest of the numbers is: " <<b<<endl;
  }
 else{
    cout << "the smallest of the numbers is: " <<c<<endl;
  }
}

您可以在一行中完成:

d = a<b? (a<c?a:c) : (b<c?b:c);
cout << "the smallest of the numbers is: " <<d<<endl;

答案 1 :(得分:0)

如果您要打印消息The smallest value among a, b, c,那么这就是您需要的

int a, b, c, d;
cout << "Enter the value of a: ";
    cin >> a;
cout << "Enter the value of b: ";
    cin >> b;
cout << "Enter the value of c: ";
    cin >> c;

if(a < b && b < c) {
    d = a;
}
else {
    if(b < a && b < c) {
        d = b;
    }
    else {
        d = c;
    }
}

cout << "the smallest value among " << a << ", " << b << " and " << c << " is: " << d << endl;