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

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

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

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

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


#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;
} // 冒泡排序

不是说了用oop吗,为什么非要用c..

题目要求用面向对象做了!用c当然能出同样的效果。但是c是面向过程的

直接比较时间复杂度会高一些,先排序。
因为生日要分年月日,所以用基数排序。
设置生日相同的学生总数初值n=1(生日相同的学生总数不可能为1,循环过之后若n=1,则n=0)
然后从小到大开始比较。
每发现后一个学生的生日和前一个学生相等,则n+=1;
基本就可以收工了。

程序没用基数排序,得有链表,不太好弄,用的是冒泡。
因为C里面没有引用,所以都是值传递的,看起来比较费劲。
初始化顺序给你打乱了,好调程序。
还有一些地方是可以优化的,但程序功能我是调试通过的。