標題:
[作業] 排序 (四)
[打印本頁]
作者:
葉桔良
時間:
2023-4-1 00:11
標題:
[作業] 排序 (四)
假設班上有五位同學, 其成績資料如下:
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
試利用選擇排序法, 為成績表加上排名.
本帖隱藏的內容需要回復才可以瀏覽
作者:
陳冠霖
時間:
2023-4-1 20:41
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int grade[5]={60,80,100,40,75};
cout<<"Original data: "<<endl;
cout<<"-------------"<<endl<<"Name\tGrade"<<endl<<"-------------"<<endl;
for(int i=0; i<5; i++)
cout<<name[i]<<"\t"<<grade[i]<<endl;
cout<<endl;
for(int i=0; i<4; i++)
{
for(int j=i+1; j<5; j++)
if(grade[i]<grade[j])
{
int tmp;
tmp = grade[i];
grade[i] = grade[j];
grade[j] = tmp;
string change;
change = name[i];
name[i] = name[j];
name[j] = change;
}
}
cout<<"Orginized by grade(highest to lowest): "<<endl;
cout<<"------------------------"<<endl<<"Name\tGrade\tLeader board"<<endl<<"------------------------"<<endl;
for(int i=0; i<5; i++)
{
cout<<name[i]<<"\t"<<grade[i]<<"\t"<<i+1;
cout<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
楊惇翔
時間:
2023-4-1 21:08
本帖最後由 楊惇翔 於 2023-4-1 21:32 編輯
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<endl;
}
cout<<endl;
cout<<"依成績排序後資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績 排名"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<4;i++)
{
for(int j=i+1; j<5; j++)
if(score[i]<score[j])
{
int tmp;
tmp = score[i];
score[i] = score[j];
score[j] = tmp;
string tmp2;
tmp2 = name[i];
name[i] = name[j];
name[j] = tmp2;
}
}
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
黃兆駿
時間:
2023-4-1 22:02
include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int grade[5]={60,80,100,40,75};
cout<<"Original data: "<<endl;
cout<<"-------------"<<endl<<"Name\tGrade"<<endl<<"-------------"<<endl;
for(int i=0; i<5; i++)
cout<<name[i]<<"\t"<<grade[i]<<endl;
cout<<endl;
for(int i=0; i<4; i++)
{
for(int j=i+1; j<5; j++)
if(grade[i]<grade[j])
{
int tmp;
tmp = grade[i];
grade[i] = grade[j];
grade[j] = tmp;
string change;
change = name[i];
name[i] = name[j];
name[j] = change;
}
}
cout<<"Orginized by grade(highest to lowest): "<<endl;
cout<<"------------------------"<<endl<<"Name\tGrade\tLeader board"<<endl<<"------------------------"<<endl;
for(int i=0; i<5; i++)
{
cout<<name[i]<<"\t"<<grade[i]<<"\t"<<i+1;
cout<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
宥竣
時間:
2023-4-2 09:57
#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;
}
複製代碼
作者:
吳柏融
時間:
2023-4-7 19:31
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<endl;
}
cout<<endl;
cout<<"依成績排序後資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績 排名"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<4;i++)
{
for(int j=i+1; j<5; j++)
if(score[i]<score[j])
{
int tmp;
tmp = score[i];
score[i] = score[j];
score[j] = tmp;
string tmp2;
tmp2 = name[i];
name[i] = name[j];
name[j] = tmp2;
}
}
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
陳昱州
時間:
2023-4-8 19:06
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int grade[5]={60,80,100,40,75};
cout<<"Original data: "<<endl;
cout<<"-------------"<<endl<<"Name\tGrade"<<endl<<"-------------"<<endl;
for(int i=0; i<5; i++)
cout<<name[i]<<"\t"<<grade[i]<<endl;
cout<<endl;
for(int i=0; i<4; i++)
{
for(int j=i+1; j<5; j++)
if(grade[i]<grade[j])
{
int tmp;
tmp = grade[i];
grade[i] = grade[j];
grade[j] = tmp;
string change;
change = name[i];
name[i] = name[j];
name[j] = change;
}
}
cout<<"Orginized by grade(highest to lowest): "<<endl;
cout<<"------------------------"<<endl<<"Name\tGrade\tLeader board"<<endl<<"------------------------"<<endl;
for(int i=0; i<5; i++)
{
cout<<name[i]<<"\t"<<grade[i]<<"\t"<<i+1;
cout<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
張晏齊
時間:
2023-4-8 19:42
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<endl;
}
cout<<endl;
cout<<"依成績排序後資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績 排名"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<4;i++)
{
for(int j=i+1; j<5; j++)
if(score[i]<score[j])
{
int tmp;
tmp = score[i];
score[i] = score[j];
score[j] = tmp;
string tmp2;
tmp2 = name[i];
name[i] = name[j];
name[j] = tmp2;
}
}
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
王述勳
時間:
2023-4-8 19:45
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"---------------"<<endl;
cout<<"姓名\t成績"<<endl;
cout<<"---------------"<<endl;
for(int i=0;i<5;i++)
cout<<name[i]<<"\t"<<score[i]<<endl;
cout<<endl<<"選擇排序後"<<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[i]<score[j])
{
int tmp1;
tmp1=score[i];
score[i]=score[j];
score[j]=tmp1;
string tmp2;
tmp2=name[i];
name[i]=name[j];
name[j]=tmp2;
}
}
}
for(int i=0;i<5;i++)
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
system("pause");
return 0;
}
複製代碼
作者:
葉權諒
時間:
2023-4-8 19:45
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<endl;
}
cout<<endl;
cout<<"依成績排序後資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名 成績 排名"<<endl;
cout<<"-------------"<<endl;
for(int i=0;i<4;i++)
{
for(int j=i+1; j<5; j++)
if(score[i]<score[j])
{
int tmp;
tmp = score[i];
score[i] = score[j];
score[j] = tmp;
string tmp2;
tmp2 = name[i];
name[i] = name[j];
name[j] = tmp2;
}
}
for(int i=0;i<5;i++)
{
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
盧禹丞
時間:
2023-4-8 19:48
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"-------------"<<endl;
cout<<"姓名\t成績"<<endl;
cout<<"-------------"<<endl;
for(int i=0; i<5; i++)
cout<<name[i]<<"\t"<<score[i]<<endl;
for(int i=0;i<4;i++)
{
for(int j=i+1; j<5; j++)
if(score[i]<score[j])
{
int tmp;
tmp = score[i];
score[i] = score[j];
score[j] = tmp;
string tmp2;
tmp2 = name[i];
name[i] = name[j];
name[j] = tmp2;
}
}
cout<<"依成績排序後資料"<<endl;
cout<<"---------------------"<<endl;
cout<<"姓名\t成績\t排名"<<endl;
cout<<"---------------------"<<endl;
for(int i=0; i<5; i++)
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
葉晉維
時間:
2023-4-8 19:52
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"--------------------"<<endl;
cout<<"姓名\t成績"<<endl;
cout<<"--------------------"<<endl;
for(int i=0;i<4;i++)
{
for(int j=i+1;j<5;j++)
{
if(score[i]<score[j])
{
int tmp1;
tmp1=score[i];
score[i]=score[j];
score[j]=tmp1;
string tmp2;
tmp2=name[i];
name[i]=name[j];
name[j]=tmp2;
}
}
}
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<5;i++)
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
system("pause");
return 0;
}
複製代碼
作者:
博勛
時間:
2023-4-8 19:54
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"--------------"<<endl;
cout<<"姓名\t成績"<<endl;
cout<<"--------------"<<endl;
for(int i=0;i<5;i++)
cout<<name[i]<<"\t"<<score[i]<<endl;
for(int i=0;i<4;i++)
{
for(int j=i+1;j<5;j++)
{
if(score[i]<score[j])
{
int temp1;
temp1 = score[i];
score[i] = score[j];
score[j] = temp1;
string temp2;
temp2 = name[i];
name[i] = name[j];
name[j] = temp2;
}
}
}
cout<<endl<<"依成績排序後資料"<<endl;
cout<<"--------------------"<<endl;
cout<<"姓名\t成績\t排名"<<endl;
cout<<"--------------------"<<endl;
for(int i=0;i<5;i++)
cout<<name[i]<<"\t"<<score[i]<<"\t"<<i+1<<endl;
system("pause");
return 0;
}
複製代碼
作者:
林哲弘
時間:
2023-4-8 19:55
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
string name[5]={"大雄","小叮噹","宜靜","技安","阿福"};
int score[5]={60,80,100,40,75};
cout<<"原始資料"<<endl;
cout<<"--------------"<<endl;
cout<<"姓名\t成績"<<endl;
cout<<"--------------"<<endl;
for(int i=0; i<5; i++)
cout<<name[i]<<"\t"<<score[i]<<endl;
for(int i=0; i<4; i++)
{
for(int j=i+1; j<5; j++)
{
if(score[i]<score[j])
{
int temp1;
temp1 = score[i];
score[i] = score[j];
score[j] = temp1;
string temp2;
temp2 = name[i];
name[i] = name[j];
name[j] = temp2;
}
}
}
cout<<"依成績排序後資料"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"姓名\t成績\t排名"<<endl;
cout<<"-----------------------------"<<endl;
for(int i=0; i<5; i++)
cout<<name[i]<<"\t"<<score[i]<<"\t"<<(i+1)<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2