- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
- int score[5]={60,80,100,40,75};
- cout<<"原始資料"<<endl<<"-------------"<<endl<<"姓名\t成績"<<endl<<"-------------"<<endl;
- for(int i=0;i<5;i++)
- cout<<name[i]<<"\t"<<score[i]<<endl;
- cout<<endl<<"依成績排序後資料"<<endl<<"--------------------"<<endl<<"姓名\t成績\t排名"<<endl<<"--------------------"<<endl;
- for(int i=0;i<4;i++)
- {
- for(int j=i+1; j<5; j++)
- if(score[i]<score[j])
- {
- int t;
- string t2;
- t = score[i];
- t2 = name[i];
- score[i] = score[j];
- name[i] = name[j];
- score[j] = t;
- name[j] = t2;
- }
- }
- for(int i=0;i<5;i++)
- cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
- system("pause");
- return 0;
- }
複製代碼 |