笔试面试相关记录(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
相关推荐
是小Y啦13 分钟前
leetcode 106.从中序与后续遍历序列构造二叉树
数据结构·算法·leetcode
LyaJpunov13 分钟前
C++中move和forword的区别
开发语言·c++
程序猿练习生18 分钟前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
liuyang-neu23 分钟前
力扣 42.接雨水
java·算法·leetcode
z千鑫26 分钟前
【人工智能】如何利用AI轻松将java,c++等代码转换为Python语言?程序员必读
java·c++·人工智能·gpt·agent·ai编程·ai工具
一名路过的小码农28 分钟前
C/C++动态库函数导出 windows
c语言·开发语言·c++
y_dd30 分钟前
【machine learning-12-多元线性回归】
算法·机器学习·线性回归
m0_6312704030 分钟前
标准c语言(一)
c语言·开发语言·算法
万河归海42831 分钟前
C语言——二分法搜索数组中特定元素并返回下标
c语言·开发语言·数据结构·经验分享·笔记·算法·visualstudio
小周的C语言学习笔记35 分钟前
鹏哥C语言36-37---循环/分支语句练习(折半查找算法)
c语言·算法·visual studio