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

TOP

返回列表