1 条题解
-
0
【例54.1】 奇数单增序列
解题思路
读入时筛出奇数,保存后升序排序,再用逗号分隔输出;重复奇数仍保留。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
输出分隔符是逗号,不是空格。
复杂度
时间O(n log n),空间O(n)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a; while (n--) { int x; cin >> x; if (x % 2) a.push_back(x); } sort(a.begin(), a.end()); for (size_t i = 0; i < a.size(); i++) { if (i) cout << ","; cout << a[i]; } cout << "\n"; return 0; }
- 1