题解 | #最后k个元素#
最后k个元素
https://www.nowcoder.com/practice/9e497c068b8a4f53b0f0576f89ff4ebe
#include<bits/stdc++.h> using namespace std; int main(){ int n,k; cin >> n >> k; vector<int>a; // write your code here...... for (int i = 0; i < n; ++i) { int num; cin >> num; a.push_back(num); } for (auto it = a.rbegin(); it != a.rbegin() + k; ++it) { cout << *it << " "; } return 0; }
使用反向迭代器反向输出即可。
C++题解 文章被收录于专栏
记录在牛客网用C++刷题的题解思路