C++ 实现三个整数从小到大排序
#include
int main() { int x, y, z; cin >> x >> y >> z; if (x > y) { int temp = x; x = y; y = temp; } if (x > z) { int temp = x; x = z; z = temp; } if (y > z) { int temp = y; y = z; z = temp; } cout << x << ' ' << y << ' ' << z << endl; return 0; }
我们先使用 cin 输入三个整数 x、y、z。
然后,我们使用三个 if 语句来进行排序,每个 if 语句判断两个数的大小关系,如果前面的数较大,则交换它们的值,确保每次比较之后,前面的数都是最小的。
最后,我们将排好序的三个数输出即可。
原文地址: https://www.cveoy.top/t/topic/lOwN 著作权归作者所有。请勿转载和采集!