笔试面试相关记录(8)

(1)

#include 

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)

#include 

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)用两个栈来实现双端队列

#include 
#include 
#include 
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 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]

#include 
#include 
#include 
using namespace std;

// a : [1, 3, 5]
// b:  [2, 4, 6]
vector getVector(string str) {
    int len = str.size();
    vector 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 num1 = getVector(str1);
        vector 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 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)

#include 
using namespace std;


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

(6)

#include 
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
#include 
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
#include 
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;
}
编译报错
#include 
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
#include 
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
#include 
using namespace std;


int main()
{
    printf("%lld", long((int*)(0)+1) - long((int*)(0)));
    return 0;
}
输出:4

你可能感兴趣的:(面试,c++,算法)