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