avirl108 2015-06-20 02:56 采纳率: 62.5%
浏览 1657
已采纳

@C语言大神:此题不用C++能做吗?求算法思路??【不要求代码】

求:同日出生的人的总个数,请使用面向对象的方法,找出若干同学中,生日相同的学生总数。
提示:
1、定义类表示日期,包含私有属性年月日,重载等于操作符,用于大小比较
2、定义类表示学生,包含私有属性姓名、生日(日期类型)
输入:四个学生对象的信息,分别是姓名,年,月,日
比如 XiaoZhang 1994 5 1
XiaoWang 1994 5 1
XiaoLi 1994 6 3
XiaoChen 1994 6 3

输出 生日相同的学生总数
比如 4

关键是不知道用C怎么做?求算法的思路??

  • 写回答

5条回答 默认 最新

  • zhangweilst 2015-06-20 11:51
    关注

    好吧。可能我写代码也上瘾了。

    
    #include <stdio.h>
    #define MAXSTUDENTS 200
    typedef struct
    {
        char* name;
        int year;
        int month;
        int day;
        int birthday;
    } stutype;
    typedef struct
    {
        stutype stu[MAXSTUDENTS];
        int count;
    } stulist;
    
    stutype assign(char* name, int year, int month, int day);
    stulist sort(stulist list);
    
    main()
    {
        stulist list;
        int n = 1;
        int i;
        int differ = 1;
        list.stu[0] = assign("XiaoZhang", 1994, 5, 1);
        list.stu[1] = assign("XiaoLi", 1994, 6, 3);
        list.stu[2] = assign("XiaoWang", 1994, 5, 1);
        list.stu[3] = assign("XiaoChen", 1994, 6, 3);
        list.stu[4] = assign("XiaoDong", 1994, 8, 1);
        list.stu[5] = assign("XiaoMing", 1994, 3, 3);
        list.stu[6] = assign("XiaoFang", 1994, 5, 1);
        list.count = 7;
        list = sort(list);
        for ( i = 0; i < list.count - 1; i++)
        {
            if ( list.stu[i + 1].birthday == list.stu[i].birthday)
            {
                n += 1;
                differ = 0;
            }
            else if ( differ == 0)
            {
                n +=1;
                differ += 1;
            }
        }
        if ( differ != 0)       // 多加了一次,去掉
            n -= 1;
    
        for ( i = 0; i < list.count; i++)
            printf("%s, %d\n", list.stu[i].name, list.stu[i].birthday);
        printf("生日相同的学生总数:%d.\n", n);
    }
    
    stutype assign(char* name, int year, int month, int day)
    {
        stutype student;
        student.name = name;
        student.year = year;
        student.month = month;
        student.day = day;
        student.birthday = 
            student.year * 10000 + student.month * 100 + student.day;
        return student;
    } // 学生结构赋值
    
    stulist sort(stulist list)
    {
        stutype temp;
        int i;
        int j;
        int finish = 0;
        for ( i = 0; i < list.count - 1 && finish == 0; i++)
        {
            finish = 1;
            for ( j = 0; j < list.count - 1; j++)
                if ( list.stu[j].birthday > list.stu[j + 1].birthday)
                {
                    temp = list.stu[j];
                    list.stu[j] = list.stu[j + 1];
                    list.stu[j + 1] = temp;
                    finish = 0;
                }
        }
        return list;
    } // 冒泡排序
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?