C++编程练习-数组逆序重放


Description
将一个数组中的值按逆序重新存放。例如,原来的顺序为8,6,5,4,1。要求改为1,4,5,6,8。
Input
输入为两行:第一行数组中元素的个数n(n大于1,n小于100)
第二行是n个整数,每两个整数之间用空格分隔。
Output
输出为一行:输出逆序后数组的整数,每两个整数之间用空格分隔。
Sample Input
5
8 6 5 4 1
Sample Output
1 4 5 6 8

参考代码

  1. #include <iostream>   
  2. #include <list>   
  3. using namespace std;  
  4. int main(){  
  5.     list<int>myList;  
  6.     int n,d;  
  7.     cin>>n;  
  8.     while(n --){  
  9.         cin>>d;  
  10.         myList.push_back(d);  
  11.     }  
  12.     myList.reverse();  
  13.     for(list<int>::iterator it = myList.begin();it != myList.end();++ it){  
  14.         cout<<*it<<" ";  
  15.     }  
  16.     cout<<endl;  
  17.     return 0;  
  18. }  

相关内容