• HW 13


    我把时间荒废了,时间便把我荒废了

                —— 莎士比亚

    Problem A: 建筑物

    题面:

    main函数:

    int main()
    {
        Building b;
        int i;
        cin>>i;
        b.setCnt(i);
        cout<<"The building has "<<b.getCnt()<<" floors."<<endl;
        return 0;
    }

    考点:类的使用

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Building
    {
    public:
        int n;
        void setCnt(int x)
        {
            n = x;
        }
        int getCnt(){return n;}
    };

    Problem B: 一个人的生日

    题面:

    main函数:

    int main()
    {
        string name;
        int year, month, day;
        cin>>year>>month>>day;
        Date date(year, month, day);
        cin>>name>>year>>month>>day;
        Person person(year, month, day, name);
        return 0;
    }

    考点:类的使用

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Date
    {
    public:
       int y,m,d;
       Date(int _y,int _m,int _d):y(_y),m(_m),d(_d)
       {
           printf("Date %d-%d-%d is created.
    ",y,m,d);
       }
       ~Date()
       {
           printf("Date %d-%d-%d is erased.
    ",y,m,d);
       }
    };
    class Person
    {
    public:
        Date da;
        string name;
        Person(int y,int m,int d,string _s):da(y,m,d),name(_s)
        {
            printf("Person ");
            cout << name;
            printf(" was born at %d-%d-%d is created.
    ",da.y,da.m,da.d);
        }
        ~Person()
        {
            printf("Person ");
            cout << name;
            printf(" was born at %d-%d-%d is erased.
    ",da.y,da.m,da.d);
        }
    };

    Problem C: 类型可变的类

    题面:

    main函数:

    int main()
    {
        Data d1;
        int i;
        cin>>i;
        Data d2(i);
        double d;
        cin>>d;
        Data d3(d);
        return 0;
    }

    考点:重载

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Data
    {
    public:
        int flag ;
        int i;
        double d;
        Data():flag(0){printf("A default object is created.
    ");}
        Data(int _d):flag(1),i(_d){
        printf("An integer object %d is created.
    ",i);
        }
        Data(double _d):flag(2),d(_d){
            cout << "A double object ";
            cout << d ;
            cout << " is created." << endl;
        }
        ~Data()
        {
            if(flag==0)
            {
                printf("The default object is erased.
    ");
            }
            else if(flag==1)
            {
                printf("The integer object %d is erased.
    ",i);
            }
            else if(flag==2)
            {
                 cout << "The double object ";
            cout << d ;
            cout << " is erased." << endl;
            }
        }
    };

    Problem D: 学校的构成(I)

    题面:

    main函数:

    int main()
    {
        int g;
        double s;
        cin>>g>>s;
        School sch(g, s);
        cin>>g;
        Student stu(g);
        cin>>s;
        Teacher tea(s);
        School(g, s);
        return 0;
    }

    考点:类的组合

    AC代码:

    include <bits/stdc++.h>
    using namespace std;
    class Student
    {
        public:
        int n;
        Student(int _n):n(_n)
        {
            printf("A student grade %d is created.
    ",n);
        }
        ~Student()
        {
            printf("A student grade %d is erased.
    ",n);
        }
    };
    class Teacher
    {
    public:
        double wage;
        Teacher(double _d):wage(_d)
        {
            printf("A teacher with salary ");
            cout << wage ;
            printf(" is created.
    ");
        }
        ~Teacher()
        {
            printf("A teacher with salary ");
            cout << wage ;
            printf(" is erased.
    ");
        }
    };
    class School
    {
    public:
        Student stu;
        Teacher tea;
        School(int _g,double _s):stu(_g),tea(_s)
        {
     
            printf("A school is created.
    ");
        }
        ~School()
        {
            printf("A school is erased.
    ");
        }
    };

    Problem E: 简单类定义

    题面:

    main函数:

    int main()
    {
        Test t1;
        int i;
        cin>>i;
     
        Test t2(i);
        cout<<"t2 is "<<t2.getMem()<<"."<<endl;
        cin>>i;
        t2.setMem(i);
        cout<<"t2 is "<<t2.getMem()<<"."<<endl;
        return 0;
    }

    考点:类的使用

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Test
    {
    public:
        int d;
        Test(int _d=0):d(_d)
        {
            printf("Test %d is created.
    ",d);
        }
        void setMem(int x)
        {
            d = x;
        }
        int getMem()
        {
            return d;
        }
        ~Test()
        {
            printf("Test %d is erased.
    ",d);
        }
    };

    Problem F: 圆的面积

    题面:

    main函数:

    int main()
    {
        double radius;
        cout<<"PI="<<Circle::PI<<endl;
        const Circle c1(2);
        cout<<"radius="<<c1.getRadius();
        cout<<",area="<<c1.getArea()<<endl;
        cin>>radius;
        Circle c2(radius);
        cout<<"radius="<<c2.getRadius();
        cout<<",area="<<c2.getArea()<<endl;
        return 0;
    }

    考点:类的使用

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Circle
    {
    public:
        double r;
        static double PI;
        Circle(double _r):r(_r)
        {
        }
        double getArea()const
        {
            return PI*r*r;
        }
        double getRadius()const
        {
            return r;
        }
    };

    Problem G: 新型乘法运算

    题面:

    main函数:

    int main()
    {
        Integer M, N;
        int a, n, m;
        cin>>a;
        M.setValue(a);
        cin>>n;
        while (n--)
        {
            cin>>m;
            N = M * m;
            cout<<N.getValue()<<endl;
        }
        return 0;
    }

    考点:先求位数,然后乘10的位数方然后再加上原来的数

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Integer
    {
    public:
        int v;
        Integer(int _v=0):v(_v){}
        void setValue(int _v)
        {
            v = _v;
        }
        int getValue()
        {
            return v;
        }
        Integer operator *(int d)
        {
            int x = 1,k = v;
            while(k)
            {
                //cout << 2 << endl;
                x*=10;
                k/=10;
            }
            k = 0;
            for(int i = 0;i<d;i++)
                k= k*x+v;
            //cout << k << endl;
            return Integer(k);
     
        }
        Integer operator =(Integer b)
        {
            v = b.v;
            return *this;
        }
    };

    Problem H: 高阶多项式(IV)

    题面:

     

    main函数:

    int main()
    {
        int n, i;
        cin>>n;
        for (i = 0; i < n; i++)
        {
            Equation eq;
            cin>>eq;
            cout<<eq;
        }
        return 0;
    }

     考点:运算符的重载,输出格式控制

    AC代码:

    int main()
    {
        int n, i;
        cin>>n;
        for (i = 0; i < n; i++)
        {
            Equation eq;
            cin>>eq;
            cout<<eq;
        }
        return 0;
    }

    Problem I: 老师的工资

    题面:

    main函数:

    int main()
    {
        int N, i, m;
        char ch;
        double basis;
        Teacher *t;
        cin>>N;
        for (i = 0; i < N; i++)
        {
            cin>>ch>>basis>>m;
            if (ch == 'h')
                t = new HighSchoolTeacher(basis, m);
            else if (ch == 'u')
                t  = new UniversityTeacher(basis, m);
            cout<<setprecision(2)<<setiosflags(ios::fixed)<<t->getSalary()<<endl;
            delete t;
        }
        return 0;
    }

    考点:类的继承,虚基类

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Teacher
    {
    public:
        double b;
        int m;
        Teacher(double _b,int _m):b(_b),m(_m)
        {
            printf("Teacher's constructor.
    ");
        }
        virtual double getSalary()=0;
        virtual ~Teacher()
        {
            printf("Teacher's deconstructor.
    ");
        }
    };
    class HighSchoolTeacher:public Teacher
    {
    public:
        HighSchoolTeacher(double _b,int _m):Teacher(_b,_m)
        {
            printf("HighSchoolTeacher's constructor.
    ");
        }
        double getSalary()
        {
            //cout << b << " " << m << endl;
            return b+m*100.0;
        }
        ~HighSchoolTeacher()
        {
            printf("HighSchoolTeacher's deconstructor.
    ");
        }
     
    };
    class UniversityTeacher:public Teacher
    {
    public:
        UniversityTeacher(double _d,int m):Teacher(_d,m)
        {
            printf("UniversityTeacher's constructor.
    ");
        }
        double getSalary()
        {
            if(m<240)
            return b + (m-240)*20;
            else if(m>240)
                return b+(m-240)*40;
        }
        ~UniversityTeacher()
        {
            printf("UniversityTeacher's deconstructor.
    ");
        }
    };
  • 相关阅读:
    【杭电ACM】1.2.4 Financial Management
    【杭电ACM】1.2.1 Biker's Trip Odometer
    【杭电ACM】1097 A hard puzzle
    【西交ACM】100 A+B problem
    【杭电ACM】1.2.2 Climbing Worm
    【杭电ACM】1004 Let the Balloon Rise
    【杭电ACM】1.2.3 hide handkerchief
    【杭电ACM】1.2.5 find your present (2)
    【杭电ACM】1.2.6 decimal system
    【西交ACM】298 第N大的数
  • 原文地址:https://www.cnblogs.com/baihualiaoluan/p/11099389.html
Copyright © 2020-2023  润新知