描述
病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序:
输入
第1行,输入一个小于100的正整数,表示病人的个数;
后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID(每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,中间用单个空格隔开。
输出
按排好的看病顺序输出病人的ID,每行一个。
样例输入
5
021075 40
004003 15
010158 67
021033 75
102012 30
样例输出
021033
010158
021075
004003
102012
#include<bits/stdc++.h>
using namespace std;
struct sick
{
char id[10];
int age;
int num;
} s,oo,yy;
struct old
{
char id[10];
int age;
int num;
}o[100];
struct young
{
char id[10];
int age;
int num;
}y[100];
int n;
int otot;
int ytot;
void swapo(int i,int j)
{
oo.age=o[i].age;
o[i].age=o[j].age;
o[j].age=oo.age;
//
strcpy(oo.id,o[i].id);
strcpy(o[i].id,o[j].id);
strcpy(o[j].id,oo.id);
//
oo.num=o[i].num;
o[i].num=o[j].num;
o[j].num=oo.num;
}
void swapy(int i,int j)
{
yy.age=y[i].age;
y[i].age=y[j].age;
y[j].age=yy.age;
//
strcpy(yy.id,y[i].id);
strcpy(y[i].id,y[j].id);
strcpy(y[j].id,yy.id);
//
yy.num=y[i].num;
y[i].num=y[j].num;
y[j].num=yy.num;
}
void sort()
{
//old
for(int i=0;i<otot;i++)
for(int j=0;j<otot-i-1;j++)
{
if(o[j].age<o[j+1].age) swapo(j,j+1);
if(o[j].age==o[j+1].age&&o[j].num>o[j+1].num) swapo(j,j+1);
}
//young
for(int i=0;i<ytot;i++)
for(int j=0;j<ytot-i-1;j++)
if(y[j].num>y[j+1].num) swapy(j,j+1);
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf(" %s %d",s.id,&s.age);
if(s.age>=60)
{
strcpy(s.id,o[otot].id);
o[otot].age=s.age;
o[otot].num=i;
otot++;
}
else
{
strcpy(s.id,y[ytot].id);
y[ytot].age=s.age;
y[ytot].num=i;
ytot++;
}
}
sort();
for(int i=0;i<otot;i++) printf("%s\n",o[i].id);
for(int i=0;i<ytot;i++) printf("%s\n",y[i].id);
return 0;
}
零输出,求救!