SDUSTOJ2058 Problem F: 一帮学生

Description

学生Student类是Person类的子类,而且每个人都有生日,生日是Date类的对象。所以,需要定义如下类:

  1. Date类:拥有年、月、日三个int类型的属性。

  2. Person类:有一个Date类型对象的属性(表示生日)、string类型属性(表示名字),以及一个int类型的静态属性numOfPersons(对象个数)。

  3. Student类:是Person类的子类,并拥有一个int类型属性(表明学生学号),一个int类型的静态属性numOfStudents(对象个数)。
    定义上述类的构造、析构函数,并根据样例输出格式输出相应的信息。

Input

第一行整数N>0表示之后有N行输入。

之后的N行,每行包括4个整数、1个字符串,分别表示年、月、日、学号和姓名。

Output

见样例~

Sample Input

3
2010 3 4 1 Tom
2010 3 5 2 Jack
2010 3 6 3 Mary

Sample Output

Date 2010-3-4 is created.
The 1th person Tom whose birthday is 2010-3-4 is created.
The 1th student Tom whose birthday is 2010-3-4 and id is 1 is created.
Date 2010-3-5 is created.
The 2th person Jack whose birthday is 2010-3-5 is created.
The 2th student Jack whose birthday is 2010-3-5 and id is 2 is created.
Date 2010-3-6 is created.
The 3th person Mary whose birthday is 2010-3-6 is created.
The 3th student Mary whose birthday is 2010-3-6 and id is 3 is created.
Student Tom whose birthday is 2010-3-4 and id is 1 is erased.
Person Tom whose birthday is 2010-3-4 is erased.
Date 2010-3-4 is erased.
Student Jack whose birthday is 2010-3-5 and id is 2 is erased.
Person Jack whose birthday is 2010-3-5 is erased.
Date 2010-3-5 is erased.
Student Mary whose birthday is 2010-3-6 and id is 3 is erased.
Person Mary whose birthday is 2010-3-6 is erased.
Date 2010-3-6 is erased.

HINT

Append Code

append.cc,

标程

#include <bits/stdc++.h>
using namespace std;
 
class Date {
    protected:
        int year, month, day;
    public:
        Date(int y, int m, int d):year(y), month(m), day(d) {
            cout << "Date " << year << "-" << month << "-" << day << " is created.\n";
        }
        ~Date() {cout << "Date " << year << "-" << month << "-" << day << " is erased.\n";}
        int gety() {return year;}
        int getm() {return month;}
        int getd() {return day;}
 
};
 
class Person {
    protected:
        Date da;
        string name;
        static int numOfPersons;
    public:
        Person(int y, int m, int d, string n):da(y, m, d), name(n) {
            cout << "The " << ++numOfPersons << "th person " << name << " whose birthday is " << da.gety() << "-" << da.getm() << "-" << da.getd() <<" is created.\n";
        }
        ~Person() {
            numOfPersons--;
            cout << "Person " << name <<" whose birthday is " << da.gety() << "-" << da.getm() << "-" << da.getd() << " is erased.\n";
        }
        int gety() {return da.gety();}
        int getm() {return da.getm();}
        int getd() {return da.getd();}
};
 
 
 
 
 
class Student:public Person{
    private:
        int id;
        static int numOfStudents;
    public:
        Student(int y, int m, int d, string n, int idd):Person(y, m, d, n), id(idd) {
            cout << "The "<<++numOfStudents<<"th student " << name << " whose birthday is " << gety() << "-" << getm() << "-" << getd() << " and id is "<<id<<" is created.\n";
        }
        ~Student() {
            numOfStudents--;
            cout << "Student " << name << " whose birthday is " << gety() << "-" << getm() << "-" << getd() <<" and id is "<<id<<" is erased.\n";
        }
};
 
int Student::numOfStudents = 0;
int Person::numOfPersons = 0;
 
int main()
{
    int year, month, day, id, i;
    string name;
    int num;
    cin>>num;
    Student **students = new Student*[num];
    for (i = 0; i < num; i++)
    {
        cin>>year>>month>>day>>id>>name;
        students[i] = new Student(year, month, day, name, id);
    }
    for (i = 0;i <num; i++)
        delete students[i];
    delete[] students;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 题目描述: 定义一个结构体,包含学生的姓名和成绩,输入多个学生的信息,按照成绩从高到低排序输出。 输入格式: 第一行输入一个整数n,表示学生的数量。 接下来n行,每行输入一个字符串和一个整数,表示学生的姓名和成绩。 输出格式: 按照成绩从高到低排序输出每个学生的姓名和成绩,每个学生的姓名和成绩之间用一个空格隔开。 如果有多个学生成绩相同,则按照姓名的字典序从小到大排序。 样例输入: 5 Tom 80 Jerry 90 Bob 80 Alice 85 John 90 样例输出: Jerry 90 John 90 Alice 85 Bob 80 Tom 80 解题思路: 本题需要用到结构体和排序,首先定义一个结构体,包含学生的姓名和成绩,然后输入多个学生的信息,将其存储在结构体数组中,最后按照成绩从高到低排序输出。 排序时需要自定义比较函数,先按照成绩从高到低排序,如果成绩相同,则按照姓名的字典序从小到大排序。 代码实现: ### 回答2: 这道题目要求我们按照学生的成绩从高到低排序,我们可以使用结构体来存储每个学生的信息,包括姓名、学号和成绩。然后我们使用冒泡排序算法来排序,要求按照成绩从高到低排列。 首先,我们需要定义一个结构体来存储学生的信息。假设我们需要存储的三个属性为name, id, score,那么我们可以这样定义结构体: ``` struct Student { char name[20]; char id[20]; int score; }; ``` 然后我们就可以定义一个数组用来存储多个学生的信息: ``` Student students[100]; ``` 接下来,我们需要读入每个学生的信息,存储到这个数组中。假设我们需要读入n个学生的信息,那么我们可以使用循环来读入每个学生的信息: ``` for (int i = 0; i < n; i++) { scanf("%s %s %d", students[i].name, students[i].id, &students[i].score); } ``` 接下来,我们使用冒泡排序算法来排序。对于每一次冒泡,我们将会通过比较相邻的两个元素,如果顺序不对就交换它们的位置。我们需要多次执行这个操作,直到所有元素都按照从大到小的顺序排序完毕。 ``` for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (students[j].score < students[j + 1].score) { Student temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } ``` 在最后,我们需要输出排序后的结果。假设我们想要输出每个学生的信息,那么我们可以使用循环来输出: ``` for (int i = 0; i < n; i++) { printf("%s %s %d\n", students[i].name, students[i].id, students[i].score); } ``` 这样就完成了问题d:结构体按成绩排序的实现。 ### 回答3: 这道题的主要任务是将一个包含学生信息的结构体按照成绩从高到低进行排序。我们可以使用冒泡排序或快速排序等算法来实现。 首先我们需要定义一个结构体,包含学生姓名、学号和成绩。 ```c++ struct Student{ string name; string num; int score; }; ``` 接下来,我们定义一个函数,该函数接收一个指向结构体数组的指针,还需要一个整数型的形参来表示数组的长度,函数的返回值为 void。该函数会根据成绩从高到低排序结构体数组。 ```c++ void sortStudents(Student* arr, int len){ for(int i = 0; i < len - 1; i++){ for(int j = i+1; j < len; j++){ if(arr[i].score < arr[j].score){ Student temp; temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } ``` 接下来,我们先在主函数中定义一个结构体数组并填充数据。 ```c++ int main(){ Student s[5] = { {"Tom", "001", 85}, {"Lily", "002", 90}, {"Bob", "003", 76}, {"Lucy", "004", 92}, {"John", "005", 88} }; // 调用函数进行排序 sortStudents(s, 5); // 输出排序结果 for(int i = 0; i < 5; i++){ cout << s[i].name << " " << s[i].num << " " << s[i].score << endl; } return 0; } ``` 运行程序后,我们可以看到按照成绩从高到低排序的结果输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值