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