笔试面试相关记录(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
相关推荐
2401_858286111 分钟前
101.【C语言】数据结构之二叉树的堆实现(顺序结构) 下
c语言·开发语言·数据结构·算法·
Beau_Will7 分钟前
数据结构-树状数组专题(1)
数据结构·c++·算法
迷迭所归处10 分钟前
动态规划 —— 子数组系列-单词拆分
算法·动态规划
爱吃烤鸡翅的酸菜鱼11 分钟前
Java算法OJ(8)随机选择算法
java·数据结构·算法·排序算法
hunandede39 分钟前
av_image_get_buffer_size 和 av_image_fill_arrays
c++
J老熊43 分钟前
JavaFX:简介、使用场景、常见问题及对比其他框架分析
java·开发语言·后端·面试·系统架构·软件工程
猿java1 小时前
什么是 Hystrix?它的工作原理是什么?
java·微服务·面试
寻找码源1 小时前
【头歌实训:利用kmp算法求子串在主串中不重叠出现的次数】
c语言·数据结构·算法·字符串·kmp
Matlab精灵1 小时前
Matlab科研绘图:自定义内置多款配色函数
算法·matlab
诚丞成1 小时前
滑动窗口篇——如行云流水般的高效解法与智能之道(1)
算法