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