现在的位置: 首页 > 综合 > 正文

九度OJ 题目1187:最小年龄的3个职工

2012年01月28日 ⁄ 综合 ⁄ 共 932字 ⁄ 字号 评论关闭
/*********************************
*   日期:2013-2-9
*   作者:SJF0115
*   题号: 九度OJ 题目1187:最小年龄的3个职工
*   来源:http://ac.jobdu.com/problem.php?pid=1187
*   结果:AC
*   来源:2003-2005年华中科技大学计算机研究生机试真题
*   总结:
**********************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Wooker{
	int ID;
	int age;
	char name[10];
}Wooker;
//关键字顺序:年龄>工号>姓名,从小到大。
int cmp(const void *a,const void *b){
	struct Wooker* c = (Wooker*) a;
	struct Wooker* d = (Wooker*) b;
	if(c->age != d->age){
		return c->age - d->age;
	}
	else if(c->ID != d->ID){
		return c->ID - d->ID;
	}
	else{
		return strcmp(c->name,d->name);
	}
}
int main()
{
	int n,i;
	//freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin);
	Wooker wooker[100];
    while(scanf("%d",&n) != EOF)
    {
		//职工号(整数), 姓名(字符串,长度不超过10), 年龄(1<=age<=100)。
		for(i = 0;i < n;i++){
			scanf("%d %s %d",&wooker[i].ID,wooker[i].name,&wooker[i].age);
		}
		//排序
		qsort(wooker,n,sizeof(wooker[0]),cmp);
		//输出结果行数为N和3的较小值
		if(n > 3){
			n = 3;
		}
		//输出
		for(i = 0;i < n;i++){
			printf("%d %s %d\n",wooker[i].ID,wooker[i].name,wooker[i].age);
		}
    }
    return 0;
}

抱歉!评论已关闭.