1 条题解
-
0
【深基3.例8】三位数排序
解题思路
把三个整数放入数组,用 sort 升序排序,再依次输出。
正确性说明
升序排序保证相邻元素不降,包含原来的全部三个数,因此就是要求的顺序。
易错点
重复数也要保留,不能用集合去重。
复杂度
固定三个数,时间 O(1),空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a[3]; cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); cout << a[0] << " " << a[1] << " " << a[2] << "\n"; return 0; }
- 1