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

TOP

返回列表