笔试面试相关记录(8)

(1)

cpp 复制代码
#include <iostream>

using namespace std;
class A {
private:
    int a1 = 0;
public:
    A() {}
    A(const A& a) {
        // cout << "A(const A& a)" << endl;
        // this->a1 = a.a1;
    }
    A operator++(int) {
        A T = *this;
        a1++; 
        cout << "++(int)" << " " << this->a1 << " " << T.a1 << endl;
        return T;
    }
    A& operator++() {
        A T = *this; 
        a1++; 
        cout << "++()" << " " << this->a1 << " " << T.a1 << endl;
        return *this;
    }
    A& operator=(const A& a) {
        cout << "=(const A& a)" << endl;
        this->a1 = a.a1;
    }
    void print() {cout << a1 << " "<< this->a1 << endl;}
};
int main()
{
    A obj;
    // 调用++(),返回*this&,输出1,在A T = *this时会调用拷贝构造函数,但是
    // 拷贝构造函数中并没有任何操作,所以T.a1 = 0, 返回*this对象调用print(),输出1 1
    (++obj).print();
    // 调用++(int),返回T,此时a1=2,A T = *this时会调用拷贝构造函数,但是
    // 拷贝构造函数中并没有任何操作,所以T.a1 = 0,输出++(int) 2 0
    obj++;
    // 输出2 2
    obj.print();
    // 调用++(int),T.a1 = 0, 输出++(int) 3 0,返回T,调用print输出0 0
    (obj++).print();
    // 调用++(),T.a1 = 0,输出++() 4 0,返回*this,调用print输出4 4
    (++obj).print();
    return 0;
}

输出:
++() 1 0
1 1
++(int) 2 0
2 2
++(int) 3 0
0 0
++() 4 0
4 4

(2)

cpp 复制代码
#include <iostream>

using namespace std;
class A {
private:
    int a1 = 0;
public:
    A() {}
    A(const A& a) {
        cout << "A(const A& a)" << endl;
        this->a1 = a.a1;
    }
    A operator++(int) {
        A T = *this;
        a1++; 
        cout << "++(int)" << " " << this->a1 << " " << T.a1 << endl;
        return T;
    }
    A& operator++() {
        A T = *this; 
        a1++; 
        cout << "++()" << " " << this->a1 << " " << T.a1 << endl;
        return *this;
    }
    A& operator=(const A& a) {
        cout << "=(const A& a)" << endl;
        this->a1 = a.a1;
    }
    void print() {cout << a1 << " "<< this->a1 << endl;}
};
int main()
{
    A obj;
    // 调用++(),返回*this&,输出1,在A T = *this时会调用拷贝构造函数,
    // 所以T.a1 = 0, a1++, 返回*this对象调用print(),输出1 1
    (++obj).print();
    // 调用++(int),返回T,此时a1=2,A T = *this时会调用拷贝构造函数,
    // T.a1 = 1,输出++(int) 2 1
    obj++;
    // 输出2 2
    obj.print();
    // 调用++(int),T.a1 = 2, 输出++(int) 3 2,返回T,调用print输出2 2
    (obj++).print();
    // 调用++(),T.a1 = 3,输出++() 4 3,返回*this,调用print输出4 4
    (++obj).print();
    return 0;
}

输出:
A(const A& a)
++() 1 0
1 1
A(const A& a)
++(int) 2 1
2 2
A(const A& a)
++(int) 3 2
2 2
A(const A& a)
++() 4 3
4 4

(3)用两个栈来实现双端队列

cpp 复制代码
#include <stack>
#include <iostream>
#include <string>
using namespace  std;

class MyDeque {
public:
    void push_back(int num) {
        stk1.push(num);
        size++;
    }

    void push_front(int num) {
        while (stk1.size()) {
            int t = stk1.top();
            stk2.push(t);
            stk1.pop();
        }
        stk1.push(num);
        while (stk2.size()) {
            int t = stk2.top();
            stk1.push(t);
            stk2.pop();
        }
        size++;
    }

    int pop_front() {
        while (stk1.size()) {
            int t = stk1.top();
            stk2.push(t);
            stk1.pop();
        }
        int res = stk2.top();
        stk2.pop();
        while (stk2.size()) {
            int t = stk2.top();
            stk1.push(t);
            stk2.pop();
        }
        size--;
        return res;
    }

    int pop_back() {
        int res = stk1.top();
        stk1.pop();
        size--;
        return res;
    }

    bool empty() const {
        return size == 0;
    }
private:
    stack<int> stk1, stk2;
    int size = 0;
};

int main() {
    string str;
    while (getline(cin, str)) {
        MyDeque mydeque;
        int len = str.size();
        int i = 0;
        // F1 F2 F3 F4 F5 B1 B2 B3 B4 B5 PF PF PB PB
        while (i < len) {
            if (str[i] == 'F') {
                int j = i+1;
                int num = 0;
                while (str[j] != ' ') {
                    num = num*10 + (str[j]-'0');
                    j++;
                }
                mydeque.push_front(num);
                i = j+1;
            } else if (str[i] == 'B') {
                int j = i+1;
                int num = 0;
                while (str[j] != ' ') {
                    num = num*10 + (str[j] - '0');
                    j++;
                }
                mydeque.push_back(num);
                i = j+1;
            } else if (str[i] == 'P' && str[i+1] == 'F') {
                cout << mydeque.pop_front() << " ";
                i += 2;
            } else if (str[i] == 'P' && str[i+1] == 'B') {
                cout << mydeque.pop_back() << " ";
                i += 2;
            } else if (str[i] == ' ') {
                i++;
            }
        }
    }
}

(4)找两个有序数组的中位数

输入:

// a : [1, 3, 5]

// b: [2, 4, 6]

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// a : [1, 3, 5]
// b:  [2, 4, 6]
vector<int> getVector(string str) {
    int len = str.size();
    vector<int> ans;
    int t = 0;
    for (int i = 0; i < len; i++) {
        if (isdigit(str[i])) {
            t = t*10 + (str[i]-'0');
        } else if (str[i] == ',') {
            ans.push_back(t);
            t = 0;
        }
    }
    ans.push_back(t);
    return ans;
}

int main() {
    string str1, str2;
    while (getline(cin, str1)) { // 注意 while 处理多个 case
        getline(cin, str2);
        vector<int> num1 = getVector(str1);
        vector<int> num2 = getVector(str2);
        // for (int i = 0; i < num1.size(); i++) {
        //     cout << num1[i] << " - ";
        // }
        // cout << endl;
        // for (int i = 0; i < num2.size(); i++) {
        //     cout << num2[i] << " = ";
        // }
        int n = num1.size();
        vector<int> total(2*n);
        int i = 0, j = 0, k = 0;
        while (i < n && j < n) {
            if (num1[i] < num2[j]) {
                total[k++] = num1[i++];
            } else {
                total[k++] = num2[j++];
            }
        }
        while (i < n) {
            total[k++] = num1[i++];
        }
        while (j < n) {
            total[k++] = num2[j++];
        }
        cout << float(total[n-1] + total[n]) / 2 << endl;
    }
}

(5)

cpp 复制代码
#include <iostream>
using namespace std;


int main()
{
    int a = 5;
    a += a*= a%= 3;
    cout << a;
    return 0;
}
输出: 8

(6)

cpp 复制代码
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    // auto f = [a, &b]()->int{return a+ ++b;};
    // int c = f();
    // cout << b;
    cout << a+ ++b << " " << a << " " << b << endl;
    a = 1;
    b = 2;
    cout << a+++b << " " << a << " " << b << endl;
    a = 1;
    b = 2;
    cout << a++ +b << " " << a << " " << b << endl;
    return 0;
}
输出:
4 1 3
3 2 2
3 2 2
cpp 复制代码
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [a, &b]()->int{return a+ ++b;};
    int c = f();
    cout << b;
    return 0;
}
输出:3
cpp 复制代码
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [a, &b]()->int{return a+++b;};
    int c = f();
    cout << b;
    return 0;
}
编译报错
cpp 复制代码
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [&]()->int{return a+++b;};
    int c = f();
    cout << a << " " << b;
    return 0;
}
输出:2 2
cpp 复制代码
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [&]()->int{return a+ ++b;};
    int c = f();
    cout << a << " " << b;
    return 0;
}
输出:1 3
cpp 复制代码
#include <iostream>
using namespace std;


int main()
{
    printf("%lld", long((int*)(0)+1) - long((int*)(0)));
    return 0;
}
输出:4
相关推荐
浮生如梦_1 小时前
Halcon基于laws纹理特征的SVM分类
图像处理·人工智能·算法·支持向量机·计算机视觉·分类·视觉检测
励志成为嵌入式工程师3 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
捕鲸叉4 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer4 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq4 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
wheeldown4 小时前
【数据结构】选择排序
数据结构·算法·排序算法
青花瓷5 小时前
C++__XCode工程中Debug版本库向Release版本库的切换
c++·xcode
观音山保我别报错5 小时前
C语言扫雷小游戏
c语言·开发语言·算法
幺零九零零6 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
TangKenny6 小时前
计算网络信号
java·算法·华为