返回列表 發帖

[作業] 排序 (四)

假設班上有五位同學, 其成績資料如下:
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};


試利用選擇排序法, 為成績表加上排名.



本帖隱藏的內容需要回復才可以瀏覽

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int t;
  6.     string name[5]= {"大雄","小叮噹","宜靜","技安","阿福"},n;
  7.     int score[5]= {60,80,100,40,75};
  8.     cout<<"原始分數:"<<endl<<" ----------"<<endl;
  9.     for(int a=0; a<=4; a++)
  10.         cout<<name[a]<<"\t"<<score[a]<<endl;
  11.     cout<<endl<<"依成績排名:"<<endl<<" ----------"<<endl;
  12.     for(int b=0; b<=4; b++)
  13.     {
  14.         for(int c=0; c<b; c++)
  15.         {
  16.             if(score[b]>score[c])
  17.             {
  18.                 t=score[b];
  19.                 score[b]=score[c];
  20.                 score[c]=t;

  21.                 n=name[b];
  22.                 name[b]=name[c];
  23.                 name[c]=n;
  24.             }

  25.         }
  26.     }
  27.     for(int a=0; a<=4; a++)
  28.         cout<<name[a]<<"\t"<<score[a]<<endl;
  29.     return 0;
  30. }
複製代碼

TOP

返回列表