1 条题解
-
0
【例52.2】 序列倒置
解题思路
输入没有长度,读到文件结束保存所有数字,再从数组末尾往前输出。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
第一个数也是数据,不是n。
复杂度
时间O(n),空间O(n)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<long long> a; long long x; while (cin >> x) a.push_back(x); for (int i = int(a.size()) - 1; i >= 0; i--) { if (i != int(a.size()) - 1) cout << " "; cout << a[i]; } cout << "\n"; return 0; }
- 1