同一天生日问题
来源:互联网 发布:计算机及网络基础知识 编辑:程序博客网 时间:2024/05/19 06:37
#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;
#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的答案了,代码大致是先定义一个结构体再用快速排序函数通过指针变量对结构题内容进行排序,再按照注释进行,最后再用一个标志检验是否有生日相同人
找到相同的生日这个方法好像可以,可以记下来,然后快速排序函数大致框架可以记下来,然后->符号是指向意思,服了自己了,,,,,,,,,,,,,,,,,,,,,,,,不明白为什么对生日也要进行排序,然后比较的是字符串长度,还有返回值也是先比较字符串,,,再去看看快速排序函数介绍好了
0 0
- 同一天生日问题
- “同一天生日”网络募捐被疑造假,为什么却成了程序员的锅?
- 366天生日密码
- 《七点三刻》046期 圣诞快乐/鲲龙成功首飞 /同一天生日募捐被查/摩拜AR单车
- 你到底哪天生日?
- 天生矛盾 (全球盗版问题随想)
- 看到这个文章:5月13日 (所属星座:金牛) 评语: 天生的吸引力
- 认识他那么容易的事那是人家那天生日
- mysql 查询年龄段,星座以及最近7天生日的sql
- 天生程序员
- 天生人语
- 天生人语
- java同一用户登录问题
- 关于shiro同一帐号同一时刻多处登录问题
- 从历史看问题,从总量思考问题,做技术的天生就规避风险,守正而不善出奇
- jQuery同一id元素 全部获取问题
- javascript 同一事件多次绑定问题详解
- 同一应用只改包名,应用跳转问题
- Mybatis学习总结(三).Mybatis全局配置文件
- wifi mesh组网
- hihoCoder 1062 : 最近公共祖先·一(map+lca)
- 「Java 8 函数式编程」读书笔记——lambda表达式
- qemu-img 的使用
- 同一天生日问题
- codeves天梯 Hannoi双塔问题
- Android 界面性能调优手册
- PrayerOJ1823: 每条边的最小生成树
- Android侧滑菜单——DrawerLayout详解使用
- 我的PAT乙级练习题1003代码记录
- 线程与条件变量和锁
- hdu 2546 饭卡( 01背包 )
- 文章标题