返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
  8.     int score[5]={60,80,100,40,75};
  9.     cout<<"原始資料"<<endl;
  10.     cout<<"---------------"<<endl;
  11.     cout<<"姓名\t成績"<<endl;
  12.     cout<<"---------------"<<endl;
  13.     for(int i=0;i<5;i++)
  14.         cout<<name[i]<<"\t"<<score[i]<<endl;
  15.     cout<<endl<<"選擇排序後"<<endl;
  16.     cout<<"---------------"<<endl;
  17.     cout<<"姓名\t成績\t排名"<<endl;
  18.     cout<<"---------------"<<endl;
  19.     for(int i=0;i<4;i++)
  20.     {
  21.         for(int j=i+1;j<5;j++)
  22.         {
  23.             if(score[i]<score[j])
  24.             {
  25.                 int tmp1;
  26.                 tmp1=score[i];
  27.                 score[i]=score[j];
  28.                 score[j]=tmp1;
  29.                 string tmp2;
  30.                 tmp2=name[i];
  31.                 name[i]=name[j];
  32.                 name[j]=tmp2;
  33.                                     
  34.             }     
  35.         }      
  36.     }
  37.     for(int i=0;i<5;i++)
  38.     cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
  39.     system("pause");
  40.     return 0;
  41. }
複製代碼

TOP

返回列表