zoukankan      html  css  js  c++  java
  • 学生成绩的快速录入(构造函数)

    现在需要录入一批学生的成绩(学号,成绩)。其中学号是正整数,并且录入时,后录入学生的学号会比前面的学号大;成绩分两等,通过(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

    #include<iostream>
    using namespace std;
    
    class Student
    {
    public:
    static int count;
    int id;
    int score;
    Student(int m_id,int m_score)//构造函数1
    {
    score=m_score;
    id=m_id;
    count++;
    }
    Student( Student &a )//构造函数2,注意传的是引用,之前一直传的是指针。。。。
    {    count++;
        id=a.id+1;
        score=a.score;
    }
    void display()
    {
        cout<<id<<' '<<(score==0? "Fail":"Pass")<<endl;
    }    
        
    };
    int Student::count=0; //静态变量的初始化
    
    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]);//注意这里*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;
    }
      
     
    不一样的烟火
  • 相关阅读:
    Object.Instantiate 实例
    .idata数据的解析
    数据结构-静态链表
    数据结构-循环链表
    Android---两个视图间的淡入淡出
    HDU 4597 Play Game 2013 ACM-ICPC吉林通化全国邀请赛H题
    Android 编译时出现r cannot be resolved to a variable
    找工作笔试面试那些事儿(5)---构造函数、析构函数和赋值函数
    SFINAE 模板替换失败而非报错的应用
    模板实参推导 & xx_cast的实现
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11083328.html
Copyright © 2011-2022 走看看