我不知道如何创建一个以数字/字母顺序打印出我的整数或字符串的程序
I dont know how to create a program that prints out my integers or strings in numerical/alphabetical order
我不知道如何制作这本书的练习,希望我以数字/字母顺序打印出三个整数或三个字符串。
我已经尝试使用if语句来解决此问题,但它只是失败了,因为我是初学者。
使用这样的代码,如果
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;
}
#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;
}
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么我的字符串变量没有打印完整的字符串?
- 我的问题是关于C++中数字和序列的重复以及如何打印它们?
- 为什么我的打印功能不起作用?链表
- 在我的变量打印C++后无法获得 $ 打印符号
- 我的链表没有按预期打印出来?
- 只有我的"链接列表"中的第一个节点正在打印
- 为什么当我尝试打印地图内容时,我得到的是地址而不是 C++ 中的值?
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 我的字符串数组一次打印出前两个字符串
- 如何让我的重载<<运算符打印出我的函数?
- 无法弄清楚为什么我的打印阵列要替换元素
- 为什么我的打印功能之一删除节点
- 为什么将我的打印方法与std::cout一起使用会导致错误
- 为什么我的打印功能为我的Postfix程序产生此错误
- 为什么我的打印水平函数不起作用?
- 为什么我的打印功能什么都不打印
- 为什么我的打印函数会吐垃圾,而我当前的函数却不能正常工作
- 为什么我的打印函数不会显示链表任一子节点中的元素?