数据结构机测四

 3378] - 数据结构实验之查找六:顺序查找

#include <stdio.h>
int main() {
    int i, n, m, k, t;
    while (~scanf("%d %d", &n, &m)) {
        t = -1;
        for (i = 0; i < n; i++) {
            scanf("%d", &k);
            if (k == m) {
                t = i;
            }
        }
        if (t == -1)
            printf("No\n");
        else
            printf("%d %d\n", t + 1, n - t);
    }
}

 

数据结构实验之查找一:二叉排序树 

struct node {
    int data;
    node *l;
    node *r;
};
int cnt;
void insert(node *&root, int num) {
    if (!root) {
        root = new node;
        root->l = root->r = NULL;
        root->data = num;
    } else {
        if (num > root->data)
            insert(root->r, num);
        else
            insert(root->l, num);
    }
}
void front(int *a, node *root) {
    if (root) {
        a[cnt++] = root->data;
        front(a, root->l);
        front(a, root->r);
    }
}

int main() {
    int n, l, num;
    while (~scanf("%d", &n)) {
        if (n == 0) break;
        scanf("%d", &l);

        int par[11];
        node *tree = NULL;
        for (int i = 0; i < n; i++) {
            cin >> num;
            insert(tree, num);
        }
        cnt = 0;
        front(par, tree);  // 找出母二叉排序树的先序遍历序列

        while (l--) {
            node *temp = NULL;
            int t[11];
            for (int i = 0; i < n; i++) {
                cin >> num;
                insert(temp, num);
            }

            cnt = 0;
            front(t, temp);  // 找出子二叉排序树的先序遍历序列
            int flag = 1;
            for (int i = 0; i < n; i++) {
                if (par[i] != t[i]) {
                    flag = 0;
                    break;
                }
            }
            printf(flag == 1 ? "Yes\n" : "No\n");
        }
    }
    return 0;
}

 

数据结构实验之查找三:树的种类统计

struct node {
    char s[22];
    int cnt;
    node *r, *l;
};
int n;
void create_tree(node *&root, char *s) {
    if (!root) {
        root = new node;
        strcpy(root->s, s);
        root->cnt = 1;
        root->l = root->r = NULL;
    } else {
        int val = strcmp(root->s, s);
        if (val > 0) {
            create_tree(root->l, s);
        } else if (val < 0) {
            create_tree(root->r, s);
        } else {
            root->cnt++;
        }
    }
}
void count(node *root) {
    if (root) {
        // 中序遍历二叉排序树就是  字典序
        count(root->l);
        printf("%s %.2lf%%\n", root->s, root->cnt * 100.0 / n);
        count(root->r);
    }
}
void to_lower_case(char *s) {
    int len = strlen(s);
    for (int i = 0; i < len; i++) {
        if (s[i] >= 'A' && s[i] <= 'Z') s[i] += 'a' - 'A';
    }
}
int main() {
    char str[22];
    cin >> n;
    getchar();
    node *tree = NULL;
    for (int i = 0; i < n; i++) {
        gets(str);
        to_lower_case(str);
        create_tree(tree, str);
    }

    count(tree);
    return 0;
}

数据结构实验之查找二:平衡二叉树

struct node {
    int data;  //记录关键字数值
    node *l, *r;
    int height;  // 该节点下的子树的高度
};
int height(node *p)  //求树的深度
{
    if (p == NULL) return -1;
    return p->height;
}
node *LL(node *p) {
    node *q = p->l;
    p->l = q->r;
    q->r = p;
    p->height = max(height(p->l), height(p->r)) + 1;
    q->height = max(height(q->l), p->height) + 1;
    return q;
}
node *RR(node *p) {
    node *q = p->r;
    p->r = q->l;
    q->l = p;
    p->height = max(height(p->l), height(p->r)) + 1;
    q->height = max(p->height, height(q->r)) + 1;
    return q;
}
node *LR(node *p) {
    p->l = RR(p->l);
    return LL(p);
}
node *RL(node *p) {
    p->r = LL(p->r);
    return RR(p);
}

void insert(node *&p, int val) {
    if (!p) {
        p = new node;
        p->data = val;
        p->l = p->r = NULL;
        p->height = 0;
    } else if (val > p->data) {
        insert(p->r, val);
        if (height(p->r) - height(p->l) == 2) {
            if (val > p->r->data)
                p = RR(p);
            else
                p = RL(p);
        }
    } else if (val < p->data) {
        insert(p->l, val);
        if (height(p->l) - height(p->r) == 2) {
            if (val < p->l->data)
                p = LL(p);
            else
                p = LR(p);
        }
    }
    p->height = max(height(p->l), height(p->r)) + 1;
}
int main() {
    int n, k;
    cin >> n;
    node *head = NULL;
    for (int i = 0; i < n; i++) {
        cin >> k;
        insert(head, k);
    }
    cout << head->data << endl;
    return 0;
}

哈希表

// 数据结构实验之查找七:线性之哈希表 
#define inf 0x3f3f3f3f
using namespace std;
int a[2000];
int Hash[2000];
int main() {
    int n, p;
    while (cin >> n >> p) {
        memset(Hash, inf, sizeof(Hash));
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            int key = a[i] % p;

            if (Hash[key] == inf) {  // 如果这个地方没有存放数据,可以存放
                Hash[key] = a[i];  // Hash 表存value
                a[i] = key;        //里面更改成存放 key
            } else {
                int d = 0;
                // 线性寻找可以存放的位置
                while (Hash[(key + d) % p] != inf) {
                    // 此数值已经被存放
                    if (Hash[(key + d) % p] == a[i]) {
                        break;
                    }
                    d++;
                }
                Hash[(key + d) % p] = a[i];
                a[i] = (key + d) % p;
            }
        }
        // 输出key
        for (int i = 0; i < n; i++) {
            printf(i == n - 1 ? "%d\n" : "%d ", a[i]);
        }
    }
    return 0;
}

 // 数据结构实验之查找五:平方之哈希表
int a[2000];
int Hash[2000];
int main() {
    int n, p;
    while (cin >> n >> p) {
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        memset(Hash, inf, sizeof(Hash));
        for (int i = 0; i < n; i++) {
            int key = a[i] % p;
            if (Hash[key] == inf) {  // 如果这个地方没有存放数据,可以存放
                Hash[key] = a[i];  // Hash 表存value
                a[i] = key;        //里面更改成存放 key
            } else {
                // 正反向寻找
                for (int j = 1;; j++) {
                    int temp = j * j;
                    if (Hash[(key + temp) % p] == inf) {
                        Hash[(key + temp) % p] = a[i];
                        a[i] = (key + temp) % p;
                        break;
                    } else if (Hash[(key - temp) % p] == inf) {
                        Hash[(key - temp) % p] = a[i];
                        a[i] = (key - temp) % p;
                        break;
                    }
                }
            }
        }

        for (int i = 0; i < n; i++) {
            printf(i == n - 1 ? "%d\n" : "%d ", a[i]);
        }
    }
    return 0;
}

数据结构实验之查找四:二分查找 

// 这个题想要ac,同样的代码,用#include<bits/stdc++.h>不能ac,
//而且会显示tle(因为这个头文件太大了,包含所有的c++函数,单用iostream不会tle)。
//如果用#include<stdio.h>就会ac。不知道为啥~!
// - -|||

#include <cstdio>
#include <iostream>
using namespace std;
int a[1000001];
int search(int l, int r, int k) {
    if (l > r) return -1;
    int mid = (l + r) / 2;
    if (a[mid] == k) return mid;
    if (a[mid] > k) return search(l, mid - 1, k);
    if (a[mid] < k) return search(mid + 1, r, k);
}
int main() {
    int n, m, k;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    while (m--) {
        scanf("%d", &k);
        printf("%d\n", search(0, n, k));
    }
    return 0;
}

 

数据结构实验之排序二:交换排序

int n, cnt1, cnt2;
void quickSort(int *c, int left, int right) {
    if (left >= right) return;
    int i = left, j = right;
    int temp = c[left];
    while (i < j) {
        while (i < j && c[j] >= temp) j--;
        if (c[i] != c[j]) cnt1++;
        c[i] = c[j];
        while (i < j && c[i] <= temp) i++;
        if (c[i] != c[j]) cnt1++;
        c[j] = c[i];
    }
    c[i] = temp;
    quickSort(c, left, i - 1);
    quickSort(c, i + 1, right);
}

void bubbleSort(int *c) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (c[j] > c[j + 1]) {
                swap(c[j], c[j + 1]);
                cnt2++;
            }
        }
    }
}

 

数据结构实验之排序三:bucket sort 

int a[101];
// cin 过不了
int main() {
    int n, d;
    scanf("%d", &n);
    memset(a, 0, sizeof(a));
    for (int i = 0; i < n; i++) {
        scanf("%d", &d);
        if (d >= 100)
            a[100]++;
        else
            a[d]++;
    }
    for (int i = 0; i <= 100; i++) {
        if (a[i] != 0) printf("%d %d\n", i, a[i]);
    }
    return 0;
}

F - 数据结构实验之排序六:希尔排序 

int a[10001];
int n;
void shellSort(int d) {
    for (int i = d; i < n; i++) {
        for (int j = i - d; j >= 0; j -= d) {
            if (a[j] > a[j + d]) swap(a[j], a[j + d]);
        }
    }
}
int main() {
    int num;

    while (~scanf("%d", &n)) {
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        shellSort(n / 2);
        for (int i = 0; i < n; i++) printf(i == n - 1 ? "%d\n" : "%d ", a[i]);
        shellSort(1);
        for (int i = 0; i < n; i++) printf(i == n - 1 ? "%d\n" : "%d ", a[i]);
    }
    return 0;
}

数据结构实验之排序四:寻找大富翁

void heapMerge(int *a, int i, int len) {
    int lc = 2 * i;
    int rc = 2 * i + 1;
    int k = i;
    // 非叶子结点  不在需要进行调整
    if (i <= len / 2) {
        if (lc <= len && a[lc] > a[k]) k = lc;
        if (rc <= len && a[rc] > a[k]) k = rc;
        if (k != i) {
            swap(a[k], a[i]);
            heapMerge(a, k, len);
        }
    }
}

void heapCreate(int *a, int n) {
    // 二叉排序树的性质  第n/2个结点一定不是叶子结点
    for (int i = n / 2; i >= 1; i--) {
        heapMerge(a, i, n);
    }
}

void heapSort(int *a, int n) {
    heapCreate(a, n);
    for (int i = n; i >= 1; i--) {
        swap(a[1], a[i]);        // 把堆顶 放到最后一个元素
        heapMerge(a, 1, i - 1);  // 继续维护大顶堆
    }
}
int main() {
    int n, m, num;
    while (~scanf("%d %d", &n, &m)) {
        int a[11];
        int p = 1;
        for (int i = 0; i < n; i++) {
            scanf("%d", &num);
            if (p <= m) {
                a[p++] = num;
            } else {
                int min = 1;
                for (int j = 1; j < p; j++) {
                    if (a[j] < a[min]) {
                        min = j;
                    }
                }
                if (a[min] < num) a[min] = num;
            }
        }
        heapSort(a, m);
        for (int i = m; i >= 1; i--) printf(i == 1 ? "%d\n" : "%d ", a[i]);
    }
    return 0;
}

数据结构实验之排序五:归并求逆序数 

using namespace std;
int a[100005], temp[100005];
long long sum;
void Merge(int s1, int e1, int s2, int e2) {
    int p = 0;
    int p1 = s1, p2 = s2;
    while (p1 <= e1 && p2 <= e2) {
        if (a[p1] <= a[p2]) {
            temp[p++] = a[p1++];
        } else {
            temp[p++] = a[p2++];
            sum += e1 + 1 - p1;
        }
    }

    while (p1 <= e1) {
        temp[p++] = a[p1++];
    }
    while (p2 <= e2) {
        temp[p++] = a[p2++];
    }
    // 回归到a
    for (int i = s1; i <= e2; i++) {
        a[i] = temp[i - s1];
    }
}
void MergeSort(int l, int r) {
    if (l < r) {
        int mid = (l + r) / 2;
        MergeSort(l, mid);
        MergeSort(mid + 1, r);
        Merge(l, mid, mid + 1, r);
    }
}
int main() {
    int n, m, num;
    while (~scanf("%d", &n)) {
        sum = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
        }
        // 注意是n-1
        MergeSort(0, n - 1);
        cout << sum << endl;
    }
    return 0;
}

 数据结构实验之排序七:选课名单

struct node {
    char s[11];
    node *next;
};
node *clist[2005];  // 记录哪些学生选了
int cla[2005];      // 记录课程人数
int main() {
    int n, m, c;
    char name[11];

    memset(cla, 0, sizeof(cla));
    for (int i = 0; i < 2005; i++) {
        clist[i] = new node;
        clist[i]->next = NULL;
    }
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++) {
        scanf("%s %d", name, &c);

        while (c--) {
            // 这一部分不能放到下面 否则会指错
            node *q = new node;
            q->next = NULL;
            strcpy(q->s, name);

            int cid;
            scanf("%d", &cid);
            cla[cid]++;
            node *p = clist[cid];
            while (p->next) {
                if (strcmp(q->s, p->next->s) < 0) break;
                // 注意 p = p->next;
                p = p->next;
            }
            q->next = p->next;
            p->next = q;
        }
    }
    for (int i = 1; i <= m; i++) {
        printf("%d %d\n", i, cla[i]);
        node *p = clist[i]->next;
        while (p) {
            cout << p->s << endl;
            // 注意 p = p->next;
            p = p->next;
        }
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值