我不知道该如何进行本书的练习,希望我能按数字/字母顺序打印出三个整数或三个字符串。
我尝试使用if语句解决此问题,但由于我是初学者,所以失败了。
使用如下代码if(a
cout << a << b << c << endl;
期望的结果是我键入但按数字顺序打印的数字。
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Enter three whole numbers" << endl; int a,b,c; cin >> a >> b >> c;
if(a<b<c) {
cout << a << "," << b << "," << c << endl;
}
if(b<a<c) {
cout << b << "," << a << "," << c << endl;
}
if(c<a<b) {
cout << c << "," << a << "," << b << endl;
}
return 0;
}
答案 0 :(得分:0)
#include <iostream>
#include <utility>
int main() {
std::cout << "Enter three whole numbers" << std::endl; int a,b,c; std::cin >> a >> b >> c;
if (a > b) std::swap(a, b); // a < b ? c
if (b > c) std::swap(b, c); // a ? b < c and a < c
if (a > b) std::swap(a, b); // a < b < c
std::cout << a << "," << b << "," << c << std::endl;
return 0;
}