数组逆序重放
2026-07-13 16:56:37
发布于:新疆
1阅读
0回复
0点赞
先看代码:
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if(!(cin >> n)) return 0;
//cin >> n;
int a[101];
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = n; i >= 1; i--) cout << a[i] << ' ';
return 0;
}
这里if(!(cin >> n)) return 0;是我的习惯,可以换成cin >> n;
剩下的就是输入遍历,倒序输出。
还有个Vector版本的,请品鉴:
#include <iostream>
#include <vector>
//#include <bits/stdc++.h> 可以用
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if(!(cin >> n)) return 0;
vector<int> a(n)//长度为n;
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = n - 1; i >= 0; i--) {
cout << a[i] << ' ';
}
return 0;
}
那么这期题解就结束了,希望对大家有所帮助!(本人已经被删三次帖了)
这里空空如也







有帮助,赞一个