鱼C论坛

 找回密码
 立即注册
查看: 651|回复: 1

为什么是错的

[复制链接]
发表于 2023-11-22 19:40:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
按学生年龄排序。输入n,然后输入n个学生信息(姓名、学号、出生日期年、月、日)按年龄排序(升序)输出。
输入
第一行整数n,第2~n+1行的每一行是一个学生的信息:学号、姓名、出生年月日
输出
已按年龄排序的学生信息,每行一个学生
样例输入
4
1002 John 2001 12 25
1001 Mary 2000 10 1
1003 Mark 2001 3 8
1004 Tony 1999 5 13
样例输出
1002 John 2001.12.25
1003 Mark 2001.3.8
1001 Mary 2000.10.1
1004 Tony 1999.5.13

#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct birth
{
        int y, m, d;
};
struct stu
{
        int num;
        string name;
        birth birth1;
};
int main()
{
        int n;
        string name1;
        cin >> n;
        int y, m, d, number;
        stu student;
        birth birthday;
        vector<stu> stus;
        for (int i = 0; i < n; i++)
        {
                cin >> number >> name1 >> y >> m >> d;
                birthday.y = y;
                birthday.m = m;
                birthday.d = d;
                student.num = number;
                student.name = name1;
                student.birth1 = birthday;
                stus.push_back(student);
        }
        for (int i = 0; i < stus.size(); i++)
        {
                for (int j = 1; j < stus.size(); j++)
                {
                        if (stus[j - 1].birth1.y < stus[j].birth1.y)
                        {
                                stu temp = stus[j - 1];
                                stus[j - 1] = stus[j];
                                stus[j] = temp;
                        }
                        else if (stus[j - 1].birth1.y == stus[j].birth1.y)
                        {
                                if (stus[j - 1].birth1.m < stus[j - 1].birth1.m)
                                {
                                        stu temp = stus[j - 1];
                                        stus[j - 1] = stus[j];
                                        stus[j] = temp;
                                }
                                else if (stus[j - 1].birth1.m == stus[j].birth1.m)
                                {
                                        if (stus[j - 1].birth1.d < stus[j].birth1.d)
                                        {
                                                stu temp = stus[j - 1];
                                                stus[j - 1] = stus[j];
                                                stus[j] = temp;
                                        }
                                }
                        }
                }
        }
        for (int i = 0; i < stus.size(); i++)
        {
                cout << stus[i].num << " " << stus[i].name << " " << stus[i].birth1.y << "." << stus[i].birth1.m << "." << stus[i].birth1.d << endl;
        }
        return 0;
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-22 19:40:16 | 显示全部楼层
这段代码存在几个问题。首先,它在比较月份和日期时存在逻辑错误。其次,排序算法也有问题。以下是修改后的代码:

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;

  6. struct birth {
  7.     int y, m, d;
  8. };

  9. struct stu {
  10.     int num;
  11.     string name;
  12.     birth birth1;
  13. };

  14. bool compare(const stu &a, const stu &b) {
  15.     if (a.birth1.y != b.birth1.y) {
  16.         return a.birth1.y < b.birth1.y;
  17.     } else if (a.birth1.m != b.birth1.m) {
  18.         return a.birth1.m < b.birth1.m;
  19.     } else {
  20.         return a.birth1.d < b.birth1.d;
  21.     }
  22. }

  23. int main() {
  24.     int n;
  25.     cin >> n;
  26.     vector<stu> stus(n);
  27.     for (int i = 0; i < n; i++) {
  28.         cin >> stus[ i].num >> stus[ i].name >> stus[ i].birth1.y >> stus[ i].birth1.m >> stus[ i].birth1.d;
  29.     }
  30.     sort(stus.begin(), stus.end(), compare);
  31.     for (int i = 0; i < n; i++) {
  32.         cout << stus[ i].num << " " << stus[ i].name << " " << stus[ i].birth1.y << "." << stus[ i].birth1.m << "." << stus[ i].birth1.d << endl;
  33.     }
  34.     return 0;
  35. }
复制代码


在这个修改后的代码中,我们引入了一个自定义的比较函数 compare ,并使用 std::sort 函数进行排序。这样可以避免原先代码中存在的逻辑错误,并且使得排序更加简洁和易读。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 20:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表