笔试面试相关记录(12)

文章展示了C++语言中的基本概念,包括函数指针、类的构造函数、输入验证、链表操作、区间合并算法以及C++继承机制。通过实例演示了如何在程序中实现这些功能并处理边界条件。
摘要由CSDN通过智能技术生成

(1)

#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)

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

(3)

#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)

x*=y+5;  等价于  x = x*(y+5);

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

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

(6) 

int j;
std::cout << (j=3,j++) << " " << std::endl;
输出3

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

输入示例

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

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

1 -> 2 -> 3 -> 4 -> 5 -> 2
5
输出0
#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]


#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值