首页 > 学院 > 开发设计 > 正文

同一天生日问题

2019-11-11 05:01:10
字体:
来源:转载
供稿:网友
   #include <stdio.h>#include <string.h>#include <stdlib.h>    struct student    {        unsigned int month, day;        char name[20];    };    int student_compare( const void* stu_a, const void* stu_b )    {        struct student* a = (struct student*)stu_a;        struct student* b = (struct student*)stu_b;        if( a->month < b->month ) return -1;        if( a->month > b->month ) return +1;        if( a->day < b->day ) return -1;        if( a->day > b->day ) return +1;        if( strlen(a->name) < strlen(b->name) ) return -1;        if( strlen(a->name) > strlen(b->name) ) return +1;        return strcmp(a->name,b->name);    }int main( ){    struct student s[180];    unsigned int n,i;    // 输入    scanf( "%u", &n );    for( i=0; i!=n; ++i )        scanf( "%s%u%u", s[i].name, &s[i].month, &s[i].day );    // 排序    qsort( s, n, sizeof(*s), &student_compare );    // 输出    _Bool bfound = 0;    for(i=0; i!=n; ++i )    {        if( i!=0 && s[i].month==s[i-1].month && s[i].day==s[i-1].day )        {            PRintf( " %s", s[i].name );        }        else if( i!=n && s[i].month==s[i+1].month && s[i].day==s[i+1].day )        {            if( bfound ) putchar( '/n' );            printf( "%u %u %s", s[i].month, s[i].day, s[i].name );            bfound = 1;        }    }    if( !bfound )        puts( "None" );    return 0;

}

我总算有个c的答案了,代码大致是先定义一个结构体再用快速排序函数通过指针变量对结构题内容进行排序,再按照注释进行,最后再用一个标志检验是否有生日相同人

找到相同的生日这个方法好像可以,可以记下来,然后快速排序函数大致框架可以记下来,然后->符号是指向意思,服了自己了,,,,,,,,,,,,,,,,,,,,,,,,不明白为什么对生日也要进行排序,然后比较的是字符串长度,还有返回值也是先比较字符串,,,再去看看快速排序函数介绍好了


上一篇:cpp 12.9

下一篇:基础练习 数列排序

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表