標題:
[作業] 排序 (四)
[打印本頁]
作者:
方浩葦
時間:
2024-8-17 11:43
標題:
[作業] 排序 (四)
假設班上有五位同學, 其成績資料如下:
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
試利用選擇排序法, 為成績表加上排名.
本帖隱藏的內容需要回復才可以瀏覽
作者:
邱博宥
時間:
2024-9-11 21:45
#include<iostream>
using namespace std;
int main()
{
int t;
string name[5]= {"大雄","小叮噹","宜靜","技安","阿福"},n;
int score[5]= {60,80,100,40,75};
cout<<"原始分數:"<<endl<<" ----------"<<endl;
for(int a=0; a<=4; a++)
cout<<name[a]<<"\t"<<score[a]<<endl;
cout<<endl<<"依成績排名:"<<endl<<" ----------"<<endl;
for(int b=0; b<=4; b++)
{
for(int c=0; c<b; c++)
{
if(score[b]>score[c])
{
t=score[b];
score[b]=score[c];
score[c]=t;
n=name[b];
name[b]=name[c];
name[c]=n;
}
}
}
for(int a=0; a<=4; a++)
cout<<name[a]<<"\t"<<score[a]<<endl;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2