C++ C+的三个整数的升序+; #包括 使用名称空间std; int main() { INTA; int b; INTC; cout>a>>b>>c; (a)&&(c>=a) { 你的病例数是对的(6种可能的顺序),但条件是错误的。我只看第一种 #include <iostream> using namespace std; int main() { int a; int b; int c; cout << "Please input three integers:" << endl; cout << ">> "; cin >> a >> b >> c; cout << endl; if (((a == a) && (b > a)) && (c >= a)) { cout << "Input three integer numbers in ascending order:" << endl; cout << a << " " << c << " " << b; } else if (((a == a) && (b >= a)) && (c >= a)) { cout << "Input three integer numbers in ascending order:" << endl; cout << a << " " << b << " " << c; } else if (((a > b) && (b == b)) && (c >= b)) { cout << "Input three integer numbers in ascending order:" << endl; cout << b << " " << c << " " << a; } else if (((a >= b) && (b == b)) && (c < b)) { cout << "Input three integer numbers in ascending order:" << endl; cout << c << " " << a << " " << b; } else if (((a > c) && (b >= c)) && (c == c)) { cout << "Input three integer numbers in ascending order:" << endl; cout << c << " " << b << " " << a; } else if (((a >= c) && (b <= c)) && (c == c)) { cout << "Input three integer numbers in ascending order:" << endl; cout << b << " " << c << " " << a; } return 0; }
(这里不需要第三个,因为根据及物性,如果C++ C+的三个整数的升序+; #包括 使用名称空间std; int main() { INTA; int b; INTC; cout>a>>b>>c; (a)&&(c>=a) { 你的病例数是对的(6种可能的顺序),但条件是错误的。我只看第一种 #include <iostream> using namespace std; int main() { int a; int b; int c; cout << "Please input three integers:" << endl; cout << ">> "; cin >> a >> b >> c; cout << endl; if (((a == a) && (b > a)) && (c >= a)) { cout << "Input three integer numbers in ascending order:" << endl; cout << a << " " << c << " " << b; } else if (((a == a) && (b >= a)) && (c >= a)) { cout << "Input three integer numbers in ascending order:" << endl; cout << a << " " << b << " " << c; } else if (((a > b) && (b == b)) && (c >= b)) { cout << "Input three integer numbers in ascending order:" << endl; cout << b << " " << c << " " << a; } else if (((a >= b) && (b == b)) && (c < b)) { cout << "Input three integer numbers in ascending order:" << endl; cout << c << " " << a << " " << b; } else if (((a > c) && (b >= c)) && (c == c)) { cout << "Input three integer numbers in ascending order:" << endl; cout << c << " " << b << " " << a; } else if (((a >= c) && (b <= c)) && (c == c)) { cout << "Input three integer numbers in ascending order:" << endl; cout << b << " " << c << " " << a; } return 0; },c++,C++,(这里不需要第三个,因为根据及物性,如果c>=a和b>=c,那么b>=a也成立。如果我正确理解您的问题,您可以选择使用算法标题中的std::sort if (( c >= a) && (b >= c)) intvals[]{3,1,2}; 标准::排序(VAL,VAL+3); 你真的想确保输入的数字是升序的。一次测试就可以确保这一点 int vals[] { 3, 1, 2 }; std::sort(vals, vals + 3); cout << v
c>=a
和b>=c
,那么b>=a
也成立。如果我正确理解您的问题,您可以选择使用算法
标题中的std::sort
if (( c >= a) && (b >= c))
intvals[]{3,1,2};
标准::排序(VAL,VAL+3);
你真的想确保输入的数字是升序的。一次测试就可以确保这一点
int vals[] { 3, 1, 2 };
std::sort(vals, vals + 3);
cout << vals[0] << vals[1] << vals[2]; // prints 123
do
{
cout>arr[0]>>arr[1]>>arr[2];
}while(!(arr[0]检查变量的自等式有什么意义?(a==a,b==b,c==c)?也许你应该试着调试你的代码?请花点时间阅读Eric Lippert的文章。@Alexandebilly这只是一个测试,你通过了!我想…顺便说一句,如果你少用一点空行,使用一个函数,你的代码可以放在两个linesTry上,让它以两个数字工作,并尝试许多组合;两者都相等,第一个tsecond。然后用三个来试试。即使答案明显错误,也要投票;)现在修好了这是正确的解决方案。两个等值数字按升序考虑,所以所有
int vals[] { 3, 1, 2 };
std::sort(vals, vals + 3);
cout << vals[0] << vals[1] << vals[2]; // prints 123
do
{
cout << "Enter 3 numbers in ascending order: ";
cin >> arr[0] >> arr[1] >> arr[2];
}while(!(arr[0] <= arr[1] && arr[1] <= arr[2]));