【C++】6-13 学生成绩的快速录入(构造函数)分数 10

6-13 学生成绩的快速录入(构造函数)

分数 10

全屏浏览

切换布局

作者 何振峰

单位 福州大学

现在需要录入一批学生的成绩(学号,成绩)。其中学号是正整数,并且录入时,后录入学生的学号会比前面的学号大;成绩分两等,通过(Pass,录入时用1代表),不通过(Fail,录入时用0代表)。

由于很多学号都是相邻的,并且学号相邻的学生成绩常常相同。所以在录入时,适当地加了速。如果当前学生的学号比前面的学号大1,且成绩与前面的成绩相同,则只输入0即可。

类定义:

完成Student类

裁判测试程序样例:

#include<iostream>
using namespace std;

/* 请在这里填写答案 */

int main(){
    const int size=100;
    int i, N, no, score;
    Student *st[size];
    cin>>N;
    for(i=0; i<N; i++){
        cin>>no;
        if(no>0){
            cin>>score;
            st[i]=new Student(no, score);
        }
        else
            st[i]=new Student(*st[i-1]);
    }
    cout<<Student::count<<" Students"<<endl;
    for(i=0;i<N;i++) st[i]->display();
    for(i=0;i<N;i++) delete st[i];
    return 0;
}

输入样例:

5
3 0
0
7 1
0
12 1

输出样例:

5 Students
3 Fail
4 Fail
7 Pass
8 Pass
12 Pass

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

class Student {
public:
    static int count; // 静态成员,用于记录创建的学生对象总数

    int no; // 学生编号
    int score; // 学生分数

    // 构造函数
    Student(int no, int score) {
        this->no = no;
        this->score = score;
        count++; // 每创建一个对象,总数加1
    }

    // 复制构造函数
    Student(const Student &other) {
        this->no = other.no+1;
        this->score = other.score;
        count++; // 即使是复制,总数也加1
    }

    // 显示学生信息的方法
    void display() {
        cout<<no<<' ';
        if(score == 0)cout<<"Fail"<<endl;
        else cout<<"Pass"<<endl;
    }

    // 析构函数(虽然在这个例子中可能不是必需的,但通常是一个好习惯)
    ~Student() {
        // 在这里可以添加释放资源或执行其他清理操作的代码
    }
};

// 初始化静态成员
int Student::count = 0;

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值