※ [本文轉錄自 NTUCivilSA 看板]
作者: jtouch (Woodstock) 看板: NTUCivilSA
標題: [公告] 計程上課練習題
時間: Mon Apr 25 17:13:09 2005
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int score;
};
void swap(student &x, student &y); //這是傳位址
int main()
{
student s[5];
s[0].name="jack";s[0].score=85;
s[1].name="john";s[1].score=84;
s[2].name="jane";s[2].score=77;
s[3].name="mike";s[3].score=89;
s[4].name="sharon";s[4].score=74;
for(int k=0;k<5;k++)
cout <<s[k].name<<" "<<s[k].score<<endl;
cout<<endl<<"ans---"<<endl;
int i=0,j=0;
do
{
do
{
if(s[j].score>s[j+1].score) swap(s[j],s[j+1]);
j++;
}while(j<4-i);
j=0;
i++;
}while(i<5);
for(k=0;k<5;k++)
cout <<s[k].name<<" "<<s[k].score<<endl;
return 0;
}
void swap(student &x, student &y)
{
student temp;
temp=x;x=y;y=temp;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59