gha890826

大一期中考-4

Apr 18th, 2020 (edited)
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void sort(int* sort_a,int num)//sort 函數
  5. {
  6.     cout<<"sorting...\n";
  7.    
  8.     for(int i=0;i<num;i++)//排序,如有不懂請找助教
  9.     {
  10.         for(int j=i+1;j<num;j++)
  11.         {
  12.             if(sort_a[i]>sort_a[j])
  13.             {
  14.                 int temp;
  15.                 temp=sort_a[i];
  16.                 sort_a[i]=sort_a[j];
  17.                 sort_a[j]=temp;
  18.             }
  19.         }
  20.     }
  21. }
  22.  
  23. int main()
  24. {
  25.     int n[5];
  26.    
  27.     cout<<"input 5 numbers:";
  28.     for(int i=0;i<5;i++)
  29.     {
  30.         cin>>n[i];
  31.     }
  32.    
  33.     //cout<<"n:"<<n<<endl;
  34.    
  35.     cout<<"n[] before sort: ";
  36.     for(int i=0;i<5;i++)
  37.     {
  38.         cout<<n[i];
  39.     }
  40.     cout<<endl;
  41.    
  42.     sort(n,5);
  43.    
  44.     cout<<"after sorted:";
  45.     for(int i=0;i<5;i++)
  46.     {
  47.         cout<<n[i];
  48.     }
  49.     cout<<endl;
  50.    
  51. }
Add Comment
Please, Sign In to add comment