08:病人排队

原题: 病人排队


建立链表
思路,从左到右历遍,如果年龄小于60,位置不变。
          年龄大于60.   从链表的第一个点开始,找到第一个比它小的,插入到该位置

代码:(AC)
#include<iostream>
#include<list>
#include<stdio.h>
using namespace std;

struct node {
	char id[11];
	int age;
};

int main()
{

	freopen("c:\\bailian\\08.txt", "r", stdin);

	int n;
	cin >> n;

	list<node>patient;
	list<node>::iterator it;
	while (n--)
	{

		node newNode;
		cin >> newNode.id;
		cin >> newNode.age;


		//如果小于60岁,直接插在末尾
		if (newNode.age < 60)
			patient.push_back(newNode);
		else
		{
			//否则,从前往后历遍
			for (it = patient.begin(); it != patient.end(); ++it)
			{
				if (it->age < newNode.age)break;
			}

			patient.insert(it, newNode);
		}
	}

	for (it = patient.begin(); it != patient.end(); ++it)
		cout << it->id << endl;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值