笔试面试相关记录(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;
}
相关推荐
AC赳赳老秦2 小时前
采集行为合规自检:OpenClaw 自动校验 robots 协议与采集频率,规避违规采集风险
java·开发语言·c++·python·php·deepseek·openclaw
无名猿3 小时前
C++ 查找算法:std::find 与容器成员 find 的性能差距
c++·性能优化·stl·标准库
程序猿编码3 小时前
工业视觉多任务方案:可编程梯度信息视觉框架,训练评估 + C++ 部署完整实战
开发语言·c++·视觉·推理引擎
朝朝辞暮i5 小时前
C++ 第 21 课:struct —— 把一组相关数据打包在一起
开发语言·c++·算法
SHARK_pssm5 小时前
【C++——类和对象(下)】
开发语言·c++·经验分享·笔记
汉克老师6 小时前
GESP2026年9月认证C++二级( 第一部分选择题(1~7题)精讲
c++·gesp·小学生·学c++编程
沫璃染墨6 小时前
《从零入门Linux系统篇(五十四):线程篇·七——互斥锁底层原理:从原子交换到线程竞争与锁实现》
linux·运维·服务器·开发语言·c++·驱动开发·系统架构
可乐ea8 小时前
GitHub Security Lab 开源 Fuzzing Taskflow:让 LLM 智能体自己跑完 C/C++ 模糊测试
jvm·c++·github·模糊测试·mcp·代码agent·智能体自动化
朝朝辞暮i8 小时前
C++ 第 13 课:值传递 —— 为什么函数里改了,外面却没变?
java·c++·算法
无忧.芙桃9 小时前
C++语言原理与实践(十):vector类的底层实现
开发语言·c++·算法