返回列表 發帖
  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<<"姓名    成績"<<endl;
  12.     cout<<"-------------"<<endl;
  13.     for(int i=0;i<5;i++)
  14.     {
  15.         cout<<name[i]<<"\t"<<score[i]<<endl;
  16.     }
  17.     cout<<endl;
  18.     cout<<"依成績排序後資料"<<endl;
  19.     cout<<"-------------"<<endl;
  20.     cout<<"姓名    成績    排名"<<endl;
  21.     cout<<"-------------"<<endl;
  22.     for(int i=0;i<4;i++)
  23.     {
  24.         for(int j=i+1; j<5; j++)
  25.             if(score[i]<score[j])
  26.             {
  27.                 int tmp;
  28.                 tmp = score[i];
  29.                 score[i] = score[j];
  30.                 score[j] = tmp;
  31.                  
  32.                 string tmp2;
  33.                 tmp2 = name[i];
  34.                 name[i] = name[j];
  35.                 name[j] = tmp2;        
  36.             }      
  37.     }
  38.     for(int i=0;i<5;i++)
  39.     {
  40.         cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
  41.     }
  42.     system("pause");
  43.     return 0;
  44. }
複製代碼

TOP

返回列表