OpenJudge 6377:生日相同 2.0——题解

6377:生日相同 2.0

总时间限制: 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
// NOI_生日相同.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct student
{
    
    
	string name;
	int year;
	int month;
};
//将结构体数组元素交换位置
void excg(student *stu, student *stu2) {
    
    
	student sdt;
	sdt = *stu;
	*stu = *stu2;
	*stu2 = sdt;
}
//将结构体数组元素按照年月排序
void sortStu(student *stu,int length) {
    
    
	for (int i = 0; i < length-1; i++) {
    
    
		for (int j = 0; j < length - 1 - i; j++) {
    
    
			if (stu[j].year > stu[j + 1].year) {
    
    
				excg(&stu[j], &stu[j+1]);
			}
			else if(stu[j].year == stu[j + 1].year&&stu[j].month>stu[j+1].month)
			{
    
    
				excg(&stu[j], &stu[j+1]);
			}
		}
	}
}
//字符串数组排序,优先比较长度其次比较Ascii码
void stringSort(string *str, int length) {
    
    
	for (int i = 0; i < length - 1; i++) {
    
    
		for (int j = 0; j < length - i - 1; j++) {
    
    
			if (str[j].length() > str[j + 1].length()) {
    
    
				string str1;
				str1 = str[j];
				str[j] = str[j + 1];
				str[j + 1] = str1;
			}
			else if (str[j].length() == str[j + 1].length()) {
    
    
				if (str[j].compare(str[j + 1]) > 0) {
    
    
					string str1;
					str1 = str[j];
					str[j] = str[j + 1];
					str[j + 1] = str1;
				}
			}
		}
	}
}
int main()
{
    
    
	int judgeFlag=0;//判断是否有生日相等的同学
	int n;
	cin >> n;
	student *stu = new student[n];
	int *flag=new int [n]();
	for (int i = 0; i < n; i++) {
    
    
		cin >> stu[i].name;
		cin >> stu[i].year;
		cin >> stu[i].month;
	}
	sortStu(stu, n);
	/*for (int i = 0; i < n; i++) {
		cout<< stu[i].name<<" ";
		cout<< stu[i].year<<" ";
		cout<<stu[i].month<<endl;
	}*/
	for (int i = 0; i < n; i++) {
    
    
		for (int j = 0; j < n; j++) {
    
    
			if (i != j && stu[i].year == stu[j].year&&stu[i].month == stu[j].month) {
    
    
				judgeFlag = 1;
				break;
			}
		}
	}
	if (judgeFlag)
	{
    
    
		for (int i = 0; i < n; i++) {
    
    
			if (flag[i] == 0)
			{
    
    
				int k = 0;
				string *name = new string[n];
				cout << stu[i].year << " " << stu[i].month;
				name[k] = stu[i].name; k++;
				flag[i] = 1;
				for (int j = 0; j < n; j++)
				{
    
    
					if (stu[i].month == stu[j].month&&stu[i].year == stu[j].year&&flag[j] == 0) {
    
    
						name[k] = stu[j].name; k++;
						flag[j] = 1;
					}
				}
				stringSort(name, k);
				for (int j = 0; j < k; j++)
				{
    
    
					cout << " " << name[j];
				}
				cout << endl;
			}
		}
	}
	else
	{
    
    
		cout << "None";
	}
	return 0;
}
/*
3
adc 7 7
add 7 7
abc 7 7
7 7 adc add abc

3
dbc 7 7
abc 7 7
bdc 7 7
7 7 dbc abc bdc
*/

猜你喜欢

转载自blog.csdn.net/weixin_44627672/article/details/112172311