返回列表 發帖

03怎樣對數值陣列由小而大排序

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     cin.tie(0);    cin.sync_with_stdio(0);

  6.     int ages[5]={11,2,3,14,5};
  7.     sort(ages,ages+5);//若只想對前3個值排序,第2個參數就是ages+3
  8.     for(int i=0;i<5;i++)
  9.         cout<<ages[i]<<" ";

  10.     return 0;
  11. }
複製代碼
May

本帖最後由 鄭繼威 於 2022-11-27 03:57 編輯

sort函式是從algorithm來的
sort(begin,end,(function))

TOP

  1. #include<iostream>
  2. #include<cmath>
  3. #include<algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7.     //cin.tie(0); cin.sync_with_stdio(0);
  8.     int ages[5]={2,8,6,7,3};
  9.     sort(ages,ages+5);
  10.     for(int j=0;j<5;j++)
  11.         cout<<ages[j]<<"";
  12.     return 0;  
  13. }
複製代碼

TOP

返回列表