题解 | #数组元素反转#
数组元素反转
https://www.nowcoder.com/practice/8c9793ae96974a9ebb153d90ef31d357
#include<iostream> #include<array> #include<algorithm> //输出格式太繁复,不如专门定义一个,本题限制了输入长度,选择array数组传递就行 void Print_Format(std::array<int,6>arr) { std::cout << "["; for (int i = 0; i < 6; i++) { if (i == 5) { std::cout << arr[i] << "]" << std::endl; break; } std::cout << arr[i] << ", "; } } int main() { std::array<int, 6> arr{}; int number{}; for (int i{}; i < 6; i++) { std::cin >> number; arr[i]=number; std::cin.clear(); } Print_Format(arr); //反转数组,直接用algorithm中的std::reverse函数即可,注意别错写成vector::reserve std::reverse(arr.begin(), arr.end()); Print_Format(arr); }