牛客网C++刷题记录过程Part4【C++语法题】

#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    for(auto it:str){
        if((it>=65 && it <=90) || (it>=97 && it <=122)){
            chars++;
        }else if (it>=48 && it <=57) {
            digits++;
        }else if (it==32) {
            whitespace++;
        }else {
            others++;
        }
    }
    

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}

         考察的是string字符串和ascii码的运用


#include <iostream>
using namespace std;

long long factorial(int n);

int main() {

    int n;
    cin >> n;

    cout << factorial(n) << endl;

    return 0;
}

long long factorial(int n) {
    if(n==1){
        return 1;
    }
    // write your code here......
    return n*factorial(n-1);
}

 递归要注意终止条件


#include <iostream>
using namespace std;

int getSum(int n);

int main() {

    int n;
    cin >> n;

    cout << getSum(n) << endl;

    return 0;
}

int getSum(int n) {
    // write your code here......
    if(n==1||n==2){
        return 1;
    }
    return getSum(n-1)+getSum(n-2);
}

 第一个月和第二个月不生兔子,则返回1对。第三个月开始生兔子,生的兔子也会生兔子,可以理解为一棵子树,即子树与父树节点层数差1,相加即可。


#include <iostream>
using namespace std;

// write your code here......
void myswap(int &a, int &b){
    int temp=a;
    a=b;
    b=temp;
}

int main() {

    int m, n;
    cin >> m;
    cin >> n;

    // write your code here......
    myswap(m, n);

    cout << m << " " << n << endl;

    return 0;
}


#include <iostream>
using namespace std;

class Cube {
private:
    int m_length;
    int m_width;
    int m_height;
    // write your code here......
public:
    void setHeight(int height){
        m_height=height;
    }
    void setLength(int length){
        m_length =length;
    }
    void setWidth(int width){
        m_width = width;
    }

    int getLength(){return m_length;}
    int getHeight(){return m_height;}
    int getWidth(){return m_width;}
    int getArea(){
        return 2*(m_height*m_width+m_height*m_length+m_width*m_length);
    }
    int getVolume(){
        return m_height*m_length*m_width;
    }

};

int main() {

    int length, width, height;
    cin >> length;
    cin >> width;
    cin >> height;

    Cube c;
    c.setLength(length);
    c.setWidth(width);
    c.setHeight(height);

    cout << c.getLength() << " "
        << c.getWidth() << " "
        << c.getHeight() << " "
        << c.getArea() << " "
        << c.getVolume() << endl;

    return 0;
}


#include <iostream>
using namespace std;

// 点类
class Pointer {

    private:
        int x;  // x 坐标
        int y;  // y 坐标

    public:
        void setX(int x) {
            this->x = x;
        }

        int getX() {
            return x;
        }

        void setY(int y) {
            this->y = y;
        }

        int getY() {
            return y;
        }

};

// 圆类
class Circle {
    friend void isPointerInCircle(Pointer p);

    private:
        Pointer center; // 圆心
        int radius; // 半径

    public:
        void setCenter(int x, int y) {
            center.setX(x);
            center.setY(y);
        }

        void setRadius(int radius) {
            this->radius = radius;
        }

        // write your code here......
        void isPointerInCircle(Pointer p){
            int x_left=p.getX()-center.getX();
            int y_left=p.getY()-center.getY();
            int distance=x_left*x_left+y_left*y_left;
            if(distance > radius*radius){
                cout<<"out"<<endl;
            }else if (distance ==radius *radius) {
                cout<<"on"<<endl;
            }else {
                cout<<"in"<<endl;
            }
        }

};

int main() {

    // 键盘输入点的坐标
    int x, y;
    cin >> x;
    cin >> y;

    // 创建一个点
    Pointer p;
    p.setX(x);
    p.setY(y);

    // 创建一个圆
    Circle c;
    c.setCenter(5, 0);
    c.setRadius(5);

    // 判断点和圆的关系
    c.isPointerInCircle(p);

    return 0;
}


#include <iostream>
#include <string>

using namespace std;

// Person类
class Person {
    public:
        string name;    // 姓名
        int age;    // 年龄

        // write your code here......
        Person(string m_name,int m_age):name(m_name),age(m_age){}
        

        void showPerson() {
            cout << name << " " << age << endl;
        }
};

int main() {

    string name;
    int age;

    cin >> name;
    cin >> age;

    Person p(name, age);
    p.showPerson();

    return 0;
}


#include <iostream>
#include <cstring>
#pragma warning(disable : 4996)
using namespace std;

class Person {

    public:
        char* name; // 姓名
        int age;    // 年龄

        Person(const char* name, int age) {
            this->name = new char[strlen(name) + 1];
            strcpy(this->name, name);
            this->age = age;
        }

        // write your code here......
        Person(const Person& p){
            this->name = new char[strlen(p.name) + 1];
            strcpy(this->name ,p.name);
            this->age = p.age;
        }
        

        void showPerson() {
            cout << name << " " << age << endl;
        }

        ~Person() {
            if (name != nullptr) {
                delete[] name;
                name = nullptr;
            }
        }

};

int main() {

    char name[100] = { 0 };
    int age;

    cin >> name;
    cin >> age;

    Person p1(name, age);
    Person p2 = p1;

    p2.showPerson();

    return 0;
}

 注意类中name变量是创建在堆区的,拷贝函数应该重新开辟一段空间,不然会导致释放内存时重复释放。


#include <iostream>
using namespace std;

class Person {
    // write your code here......
    friend void showAge(Person &p);

    public:
        Person(int age) {
            this->age = age;
        }

    private:
        int age;
};

void showAge(Person& p) {
    cout << p.age << endl;
}

int main() {

    Person p(10);
    showAge(p);

    return 0;
}


#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        Time operator+(Time& t){
            Time res;
            res.hours=this->hours+t.hours;
            res.minutes = this->minutes + t.minutes;
            if(res.minutes>=60){
                res.minutes -=60;
                res.hours++;
            }
            return res;
        }
        

};

int main() {

    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(2, 20);

    Time t3 = t1 + t2;
    t3.show();
    
    return 0;
}

 重载形式 :  [返回值类型] operator[符号][(参数列表)]


#include <iostream>
using namespace std;

class Base {

    private:
        int x;
        int y;

    public:
        Base(int x, int y) {
            this->x = x;
            this->y = y;
        }

        int getX() {
            return x;
        }

        int getY() {
            return y;
        }

};

class Sub : public Base {

    private:
        int z;

    public:
        Sub(int x, int y, int z):Base(x,y)  {
            // write your code here
            this->z=z;    
        }

        int getZ() {
            return z;
        }

        int calculate() {
            return Base::getX() * Base::getY() * this->getZ();
        }

};

int main() {

    int x, y, z;
    cin >> x;
    cin >> y;
    cin >> z;
    Sub sub(x, y, z);
    cout << sub.calculate() << endl;
    
    return 0;
}

 子类构造函数中若有父类成员变量需要初始化,可以直接调用父类构造函数放入子类构造参数列表后面,中间加上 :


#include <iostream>
using namespace std;

class Base {

private:
    int x;
    int y;

public:
    Base(int x, int y) {
        this->x = x;
        this->y = y;
    }

    int getX() {
        return x;
    }

    int getY() {
        return y;
    }

    void calculate() {
        cout << getX() * getY() << endl;
    }

};

class Sub : public Base {
// write your code here......
public:
    Sub(int x,int y):Base(x,y){}

    void calculate(){
        if(Base::getY()==0){
            cout<<"Error"<<endl;
            return;
        }
        cout<<Base::getX()/Base::getY()<<endl;
    }
};

int main() {

    int x, y, z;
    cin >> x;
    cin >> y;
    Sub sub(x, y);
    sub.calculate();
    
    return 0;
}

开始涉及到类了,类的细节很多,简单如封装,继承比较好理解,也是今天写的,包括友元函数也非常简单,后面可能涉及到多态,虚函数等等,可能难度会有所提高。

都是上课摸鱼写的~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值