相同生日

【问题描述】

在一个有200人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的学号,出生月日,试找出所有生日相同的学生。

【输入形式】

第一行为整数n,表示有n个学生,n<=200。此后每行包含一个字符串和两个整数,分别表示学生的学号(字符串长度为11位)和出生月(1<=m<=12)日(1<=d<=31),学号、月、日之间用一个空格分隔。

【输出形式】

对每组生日相同的学生,输出一行,其中前两个数字表示月和日,后面跟着所有在当天出生的学生的学号,数字、学号之间都用一个空格分隔。对所有的输出,要求按日期从前到后的顺序输出。对生日相同的学号,按输入的顺序输出。

【样例输入】

6
07101020105 3 15
07101020115 4 5
07101020118 3 15
07101020108 4 5
07101020111 4 5
07101020121 8 10
【样例输出】

3 15 07101020105 07101020118
4 5 07101020115 07101020108 07101020111
8 10 07101020121


#include<iostream>
using namespace std;
struct stu{
    string str;
    int month;
    int day;
    int time;
};

int main()
{
    int n;
    cin>>n;
    stu student[n];
    for(int i=0;i<n;i++)
    {
        cin>>student[i].str>>student[i].month>>student[i].day;
        student[i].time=i;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(student[j].month<student[i].month)
            {
                stu x;
                x=student[i];
                student[i]=student[j];
                student[j]=x;
            }
            if(student[j].month==student[i].month&&student[j].day<student[i].day)
            {
                stu x;
                x=student[i];
                student[i]=student[j];
                student[j]=x;
            }
            if(student[j].month==student[i].month&&student[j].day==student[i].day&&student[i].time>student[j].time)
            {
                stu x;
                x=student[i];
                student[i]=student[j];
                student[j]=x;
            }
        }   
    }
    int step=1;//超前一步 
    int flag=0;
    int stop=0;
    while(stop<n)
    {
        while(stop+step<n&&student[stop].month==student[stop+step].month
               &&student[stop].day==student[stop+step].day)
        {
            step++;
            flag=1;
        }
        if(flag==1)
        {
            cout<<student[stop].month<<" "<<student[stop].day<<" ";
            for(int j=stop;j<stop+step;j++)
            {
                cout<<student[j].str<<" ";
            }
            cout<<endl;
            stop=stop+step;
            step=1;
        }
        else
        {
            cout<<student[stop].month<<" "<<student[stop].day<<" "<<student[stop].str<<endl;
            stop++;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzydadong/article/details/82656598