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

TOP

返回列表