深信服2018笔试瞎写版

第一题:堆排序

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

void build_max_heap(int arr[], int curr, int size) {
    if (curr >= size || 1 + curr * 2 >= size) { return; }
    int root = curr;
    int lchild = 1 + curr * 2;
    int rchild = lchild + 1;
    int maxNum = lchild;
    if (rchild < size && arr[rchild] > arr[lchild]) { maxNum = rchild; }
    if (arr[maxNum] > arr[root]) {
        int t = arr[root];
        arr[root] = arr[maxNum];
        arr[maxNum] = t;
        build_max_heap(arr, maxNum, size);
    }
    return;
}

void heap_sort(int a[], int n) {
    build_max_heap(a, 0, n);
    for (int i = (n / 2) - 1; i >= 0; i--) { build_max_heap(a, i, n); }

    for (int i = n - 1; i >= 0; i--) {
        int t = a[0]; a[0] = a[i]; a[i] = t;
        build_max_heap(a, 0, i);
    }
}

int main(void)
{
    int* arr;
    int n;
    cin >> n; 
    arr = new int[n];
    for (int i = 0; i < n; i++) { cin >> arr[i]; }
    heap_sort(arr, n);
    for (int i = 0; i < n; i++) { cout << arr[i] << " \n"[arr[i] == n]; }

    return 0;
}

单链表的冒泡

#include <iostream>

using std::cin;
using std::cout;

struct node {
    node* next;
    int val;
};

void bub_sort(node* root) {
    node* now = root -> next;
    while (now != nullptr) {
        node* point = now -> next;
        while (point > nullptr) {
            if (now -> val > point -> val) {
                int t = now -> val;
                now -> val = point -> val;
                point -> val = t;
            }
            point = point -> next;
        }
        now  = now -> next;
    }

    return;

}

int main(void)
{
    node* root = new node;
    int n;
    cin >> n;

    node* now = root;
    while (n--) {
        node* tmp = new node;
        tmp -> next = nullptr;
        cin >> tmp -> val;
        now -> next = tmp;
        now = now -> next;
    }
    bub_sort(root);

    now = root -> next;
    while (now != nullptr) {
        cout << now -> val << " \n"[now -> next == nullptr] ;
        now = now -> next;
    }

    return 0;
}

无符号数的二进制倒序

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

unsigned int rev(unsigned int x) {
    unsigned rt = 0;
    for (int i = 0; i < 32; i++) {
        rt = rt << 1;
        rt += x % 2;
        x = x >> 1;
    }
    return rt;
}

int main(void)
{
    unsigned int x;
    cin >> x;
    cout << rev(x) << endl;
    return 0;
}

字符串匹配

#include <iostream>
#include <cstring>

using std::cin;
using std::cout;
using std::endl;

int match(char sa[], char sb[]) {   
    if (strlen(sa) == 0 && strlen(sb) == 0) { return 0; }
    if (strlen(sa) == 0 || strlen(sb) == 0) { return -1; }  
    if (sb[0] == '?') { return match(sa + 1, sb + 1); }
    if (sb[0] == '*') {
        for (int i = 0; i <= strlen(sa); i++) {
            if (0 == match(sa + i, sb + 1)) { return 0; }
        }
        return -1;
    }
    if (sb[0] == sa[0]) { return match(sa + 1, sb + 1); }
    return -1;
}

int main(void)
{
    char str0[256], str1[256];
    cin >> str0 >> str1;
    cout << match(str0, str1) << endl;
    return 0;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值