• 生日相同


    题目链接:http://ica.openjudge.cn/struct/1/

    总时间限制: 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
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 struct stu
     5 {
     6     int month,day;//月,日
     7     char name[25]; 
     8 };
     9 struct obj
    10 {
    11     int month,day,count;//该日期生日的人数是count 
    12 };
    13 int cmp(const void *a,const void *b)//需要交换的返回1,否则返回-1或0 
    14 {
    15     struct stu x,y;
    16     int len1,len2;
    17     x=*((struct stu *)a);
    18     y=*((struct stu *)b);
    19     if(x.month>y.month)return 1;//需要交换 
    20     else if(x.month<y.month) return 0;//不需要交换 
    21     else if(x.day>y.day)return 1;
    22     else if(x.day<y.day) return 0;
    23     else 
    24     {
    25         len1=strlen(x.name);
    26         len2=strlen(y.name);
    27         if(len1>len2)return 1;
    28         else if(len1<len2)return 0;
    29         else return strcmp(x.name,y.name);//长度相等,按字典序升序排序。返回正数表示要交换,返回0或负数不交换 
    30     }
    31 }
    32 int main(int argc, char *argv[])
    33 {
    34     int n,i,j,k,t;
    35     struct stu a[200];
    36     struct obj b[200];
    37     freopen("data.in","r",stdin);
    38     
    39     scanf("%d",&n);
    40     for(i=0;i<n;i++)
    41     {
    42         scanf("%s%d%d",&a[i].name,&a[i].month,&a[i].day);
    43     }
    44     
    45     qsort(a,n,sizeof(struct stu),cmp);
    46     //for(i=0;i<n;i++)
    47         //printf("%d %d %s
    ",a[i].month,a[i].day,a[i].name);
    48     
    49     b[0].month=a[0].month;
    50     b[0].day=a[0].day;
    51     b[0].count=1;
    52     j=0;
    53     for(i=1;i<n;i++)
    54     {
    55         if(a[i].month==b[j].month&&a[i].day==b[j].day) b[j].count++;
    56         else
    57         {
    58             j++;
    59             b[j].month=a[i].month;
    60             b[j].day=a[i].day;
    61             b[j].count=1;
    62         }
    63     }
    64     
    65     if(j==(n-1)) printf("None
    ");
    66     else
    67     {
    68         k=0;
    69         for(i=0;i<=j;i++)
    70         {
    71             if(b[i].count>1)
    72             {
    73                 printf("%d %d",b[i].month,b[i].day);
    74                 for(t=0;t<b[i].count;t++)
    75                 {
    76                     printf(" %s",a[k].name);
    77                     k++;
    78                 }
    79                 printf("
    ");
    80             }
    81             else k++;
    82         }
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    HTML中CSS入门基础
    HTML基本代码教学,第三天
    HTML基本代码教学,第二天
    HTML基本代码教学片,认识HTML
    开学第一天,规章制度,教学大纲
    新的学期,从头开始
    开启新模式WinForm
    封装、继承、多态的基本详细使用方式与方法以及含义
    Python开发基础-Day4-布尔运算、集合
    Python开发基础-Day3-列表、元组和字典
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/7245773.html
Copyright © 2020-2023  润新知