歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++編程練習-數組逆序重放

C++編程練習-數組逆序重放

日期:2017/3/1 11:16:40   编辑:Linux編程

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. }
Copyright © Linux教程網 All Rights Reserved