所以我正在开发这个项目,我必须从用户那里收集 2 个整数 3 次(循环),并且每次我都必须按升序打印这两个整数。限制是循环中只能有两个 cout 语句(一个要求输入,第二个输出升序)。
我唯一的问题是,当我考虑升序时,我会这样做(有两个计数语句):
if (m<n) {
cout << m << n << endl;
if (m>n){
cout << n << m << endl;
到目前为止,这就是我所拥有的:
#include <iostream>
using namespace std;
int main(int,char**) {
int n, m, z;
for (n=0;n<3;n++){
cout << "Give me two numbers: ";
cin >> m;
cin >> z;
//if (m>z);
//cout << m << z << "sorted is: " << m << z << endl;
// This is where I'm getting stuck because I need two count statements to organize in ascending order as shown above
}
}
最佳答案
那么您是否考虑过更改哪个变量保存较小的数字?例如
if(m > n){
int temp = n;
n = m;
m = temp;
}
然后你就可以只使用一张打印
cout << m << " " << n << endl;
关于c++ - 在C++中按升序打印多个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46822227/