题目要求:
(http://noi.openjudge.cn/ch0301/6377/)
总时间限制:
1000ms
内存限制:
65536kB
描述
在一个有180人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的名字,出生月日。试找出所有生日相同的学生。
输入
第一行为整数n,表示有n个学生,n ≤ 180。此后每行包含一个字符串和两个整数,分别表示学生的名字(名字第一个字母大写,其余小写,不含空格,且长度小于20)和出生月(1 ≤ m ≤ 12)日(1 ≤ d ≤ 31)。名字、月、日之间用一个空格分隔
输出
每组生日相同的学生,输出一行,其中前两个数字表示月和日,后面跟着所有在当天出生的学生的名字,数字、名字之间都用一个空格分隔。对所有的输出,要求按日期从前到后的顺序输出。 对生日相同的名字,按名字从短到长按序输出,长度相同的按字典序输出。如没有生日相同的学生,输出”None”
样例输入
6 Avril 3 2 Candy 4 5 Tim 3 2 Sufia 4 5 Lagrange 4 5 Bill 3 2
样例输出
3 2 Tim Bill Avril 4 5 Candy Sufia Lagrange
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
struct student
{
char id[25];
int month, date;
int xu;
};
bool cmp(student a,student b)
{
int x=strlen(a.id),y=strlen(b.id);
if(a.month == b.month )
{
if(a.date == b.date)
{
if(x==y)
{
return strcmp(a.id,b.id)<0;
}
return x<y;
}
else
{
return a.date < b.date ;
}
}
return a.month < b.month ;
}
int main()
{
student stu[185];
int n;
bool isBegin = true, isFirst = true;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s %d %d", &stu[i].id, &stu[i].month, &stu[i].date);
stu[i].xu = i ;
}
sort(stu, stu+n, cmp);
for(int i = 0; i < n-1; i++)
{
if(stu[i].month == stu[i+1].month && stu[i].date == stu[i+1].date)
{
if(isBegin)
{
if(isFirst)
{
printf("%d %d %s", stu[i].month, stu[i].date, stu[i].id);
isFirst = false;
}
else
printf("\n%d %d %s",stu[i].month,stu[i].date,stu[i].id);
isBegin = false;
}
printf(" %s",stu[i+1].id);
}
else
isBegin = true;
}
if(isBegin==true)
printf("None\n");
return 0;
}