笔试面试相关记录(12)

(1)

cpp 复制代码
#include <stdlib.h>

int test(int *a) {
    int* t;
    *a = 99;
    printf("%p---\n", a);
    t = a;
    return t;
}
 
int main()
{
    int x, *y;
    printf("%p=%d\n", &x,x);
    y = test(&x);
    printf("%p", y);
    return(0);
}

(2)

cpp 复制代码
#include <iostream>
 
class CA {
public:
    CA(){};
    CA(int val){};
};
int main()
{
    CA a;
    CA a1();
    CA a2(100);
    CA a3 = 100;
    return 0;
}

(3)

cpp 复制代码
#include <iostream>

using namespace std;

int main(){
	int x,y;
    while(1) {
        scanf("%d, %d", &x, &y);
        cout << x << " " << y << endl;
    }
	return 0;
}
只有输入11, 12 时,才会输出11 12
必须有逗号,空格可以多个或者没有,如果输入12 34
则输出
12 xxxx
34 xxxx


int main(){
	int x,y;
    while(1) {
        scanf("%d %d", &x, &y);
        cout << x << " " << y << endl;
    }
	return 0;
}
必须得输入空格,空格可以有多个

(4)

cpp 复制代码
x*=y+5;  等价于  x = x*(y+5);

(5)派生类向基类传递参数_当c++派生类作为参数传递给基类参数函数-CSDN博客

C++ 多重继承、虚继承与虚基类-CSDN博客

(6)

cpp 复制代码
int j;
std::cout << (j=3,j++) << " " << std::endl;
输出3

(7)输入链表,和一个数字n,输入链表中倒数第n个数字,如果链表有环,则输出0,如果链表没环,但是n超过了链表长度,则输出-1,否则输出对应的数字。

输入示例

cpp 复制代码
1 -> 2 -> 3 -> 4 -> 5
4
输出2

1 -> 2 -> 3 -> 4 -> 5
10
输出-1

1 -> 2 -> 3 -> 4 -> 5 -> 2
5
输出0
cpp 复制代码
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

int main() {
    string str;
    while (getline(cin, str)) { // 注意 while 处理多个 case
        int n;
        cin >> n;
        // 接受cin中残留的回车,这个很重要
        getchar();
        map<int, int> mp;
        vector<int> nums;
        int len = str.size();
        int t = 0;
        bool flag = false;
        bool hasLoop = false;
        for (int i = 0; i < len; i++) {
            if (isdigit(str[i])) {
                t = t*10 + (str[i]-'0');
                flag = true;
            } else {
                if (flag) {
                    if (mp[t]) {
                        // 有环
                        cout << 0 << endl;
                        hasLoop = true;
                        break;
                    }
                    nums.push_back(t);
                    mp[t] = 1;
                    t = 0;
                    flag = false;
                }
            }
        }
        if (flag) {
            if (mp[t]) {
                // 有环
                cout << 0 << endl;
                hasLoop = true;
            }
            nums.push_back(t);
            mp[t] = 1;
        }
        if (!hasLoop) {
            int numSize = nums.size();
            if (n > numSize) {
                cout << -1 << endl;
            } else {
                cout << nums[numSize-n] << endl;
            }
        }
        

    }
}

(8)输入多个数组区间,合并之后,找出缺失的区间,如果没有输出-1

输入:2,6;8,10;1,3;15,18

输入:6, 8

10, 15

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

static bool cmp (const vector<int> &a, const vector<int>& b) {
    return a[0] < b[0];
}

int main () {
    string str;
    while (getline(cin, str)) {
        int len = str.size();
        int t = 0;
        vector<vector<int>> nums;
        bool hasNum = false;
        int start;
        for (int i = 0; i < len; i++) {
            if (isdigit(str[i])) {
                t = t*10 + (str[i]-'0');
                hasNum = true;
            } else {
                if (hasNum) {
                    if (str[i] == ',') {
                        start = t;

                    } else if (str[i] == ';') {
                        nums.push_back({start, t});
                    }
                    t = 0;
                    hasNum = false;
                }
            }
        }
        // 2,6;8,10;1,3;15,18
        if (hasNum) {
            nums.push_back({start, t});
        }
        sort(nums.begin(), nums.end(), cmp);
        int lastStart = nums[0][0];
        int lastEnd = nums[0][1];
        vector<vector<int>> newNums;
        for (int i = 1; i < nums.size(); i++) {
            if (nums[i][0] <= lastEnd) {
                lastEnd = max(lastEnd, nums[i][1]);
            } else {
                newNums.push_back({lastStart, lastEnd});
                lastStart = nums[i][0];
                lastEnd = nums[i][1];
            }
        }
        newNums.push_back({lastStart, lastEnd});
        if (newNums.size() == 1) {
            cout << -1 << endl;
        } else {
            for (int i = 0; i < newNums.size()-1; i++) {
                cout << '[' << newNums[i][1] << ',' << ' ' << newNums[i+1][0] << ']' << endl;
            }
        }   
        
    }
    return 0;
}
相关推荐
nLif几秒前
基于GTK的简易MFC对话框兼容层 -- MfcToGTK
linux·c++·mfc·gtk
小小、码农2 小时前
C++11右值引用与移动语义 —— 从拷贝资源到转移资源
开发语言·网络·c++·网络协议
charlie1145141914 小时前
C++ 的新标准容器:flat_map、inplace_vector 与 mdspan
开发语言·c++·开源项目
唠玖馆4 小时前
c++AVL树
开发语言·c++
蒸蒸yyyyzwd5 小时前
CPP 选手秋招学习笔记 day31
c++·求职招聘
linx2955 小时前
单元三 · 那 C 的底层知识怎么办
c语言·开发语言·数据结构·c++·嵌入式硬件
汉克老师6 小时前
GESP 六级明日冲刺:最后一晚只抓两个大方向
c++·gesp·小学生·学c++编程
神仙别闹6 小时前
基于 QT(C++)开发的地铁换乘系统
数据库·c++·qt
Dr_Fourier7 小时前
mmap的使用与底层原理
linux·c++