C语言程序设计-同一天生日[综合应用]

【问题描述】

在一个有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

//============================================================================
// Name        : 10061_c.cpp
// Author      : coder
// Version     : 基本思想:以月份 *31 +天数作为索引
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//学号
char nums[201][50];
//月,日
int month[201],day[201];
int dayIndex[400][200];
int indexLen[400];//记录key值出现的次数
int main() 
{
    int n;
    scanf("%d",&n);
    int M=31; //最大每月天数
    int i;
    for(i=0; i<400; i++) 
        indexLen[i]=0;

    for(i=1; i<=n; i++)
    {
        scanf("%s %d %d", nums[i], &month[i], &day[i]);
        int index = (month[i]-1)*M + day[i];
        dayIndex[index][indexLen[index]] = i;
        indexLen[index]++;
    }
    for(i=1; i<=12*31; i++)
    {
        if(indexLen[i])
        {
            printf("%d %d ", i/31+1, i%31);//打印月,天
            for(int j=0; j<indexLen[i]; j++)
                printf("%s ",nums[dayIndex[i][j]]);
            puts("");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/love533/p/3433523.html