病人排队

病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序: 
1. 老年人(年龄 >= 60岁)比非老年人优先看病。 
2. 老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。 
3. 非老年人按登记的先后顺序看病。

输入
第1行,输入一个小于100的正整数,表示病人的个数;
后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID(每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,中间用单个空格隔开。
输出
按排好的看病顺序输出病人的ID,每行一个。
样例输入
5
021075 40
004003 15
010158 67
021033 75
102012 30
 
 
 
 
参考代码
#include <string.h>
#include <string>
using std::string;

#include <iostream>
using namespace std;

struct paitent{
	int age;
	char id[10];
	int sequence;
};

int Compare(paitent a, paitent b){
	if (a.age >= 60 && b.age >= 60){
		if (a.age < b.age){
			return true;
		}
		else if (a.age == b.age){
			if (a.sequence > b.sequence ){
				return true;
			}
		}
	}
	if (a.age < 60 && b.age < 60) {
		if (a.sequence > b.sequence){
			return true;
		}
	}
	if  (a.age < 60 && b.age >= 60) {
		return true;
	}

	return false;
}

void PrintResult(paitent *s,int n){
	for (int i =0;i<n;i++){
		cout << s[i].id << endl;
	}
}

void PaitentSort (paitent *s,int num){
	paitent tmp;
	for(int i=0 ; i < num-1;i++){
		for (int j=0;j<num-1-i;j++){
			if (Compare(s[j],s[j+1])){
				tmp = s[j];
				s[j] = s[j+1];
				s[j+1] = tmp ;
			}
		}
	}
}




int main()
{
    //freopen("C:\\Users\\Administrator\\Desktop\\mooc.txt","r",stdin);
	int n;
	paitent s[100];
	cin >> n ;
	for (int i =0;i<n;i++){
		s[i].sequence = i;
		cin >> s[i].id >>s[i].age;
	}

	PaitentSort(s,n);
	PrintResult(s,n);
	return 0;
}


样例输出
021033
010158
021075
004003
102012
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值