HLG 1164 ,1287 数字去重和排序(二叉排序树)

链接1:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1164

链接2:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1287

Description:

用计算机随机生成了N个0到910305(包含0和910305)之间的随机整数(N≤100000000),对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再把这些数从小到大排序。
请你完成“去重”与“排序”的工作。

输入有2行,第1行为1个正整数,表示所生成的随机数的个数:
N
第2行有N个用空格隔开的正整数,为所产生的随机数。

输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。

Sample Input:

10
20 40 32 67 40 20 89 300 400 15

Sample Output:

8
15 20 32 40 67 89 300 400


The following code :


#include <iostream>
#include <cstdio>
#include <cstdlib>
#define MAXN 100000005
using namespace std;

int Kesha[MAXN], Kay[MAXN];

int cmp(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

int main() {
    int n;
    while(~scanf("%d", &n)) {
        for(int i=0; i<n; i++) {
            scanf("%d", &Kesha[i]);
        }
        qsort(Kesha, n, sizeof(int), cmp);
        int k = 0;
        for(int i=0; i<n-1; i++) {
            if(Kesha[i] != Kesha[i+1]) {
                Kay[k++] = Kesha[i];
            }
        }
        Kay[k++] = Kesha[n-1];
        printf("%d\n", k);
        for(int i=0; i<k; i++) {
            printf("%d", Kay[i]);
            i == k-1 ? (printf("\n")) : (printf(" "));
        }
    }
    return 0;
}

Description用计算机随机生成了N个0到1000000000(包含0和1000000000)之间的随机整数(N≤5000000),对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再把这些数从小到大排序。
请你完成“去重”与“排序”的工作Input输入有2行,第1行为1个正整数,表示所生成的随机数的个数:
N
第2行有N个用空格隔开的正整数,为所产生的随机数。Output输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。Sample Input10
20 40 32 67 40 20 89 300 400 15Sample Output

8
15 20 32 40 67 89 300 400


The following code :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int b[5000001], top; 

typedef struct Node {
    int count;
    struct node *left ;
    struct node *right;
}node,*trie;

void init(trie &p)
{
    p = (trie)malloc(sizeof(node));
    p->count = -1;
    p->left = NULL;
    p->right = NULL;
}
void add(trie &p, int m)
{
    if(m == p->count)
        return;
    if(p->count==-1) {
        p->count = m;
        return;
    }
    if(m < p->count) {
        trie q;
        q = p->left;
        if(q == NULL) {
            init(q);
            p->left = q;
        }
        add(q, m);
    }else {
        trie q;
        q = p->right;
        if(q == NULL) {
            init(q);
            p->right = q;
        }
        add(q, m);
    }
}

void get(trie &p)
{
    if(p) {
        get(p->left);
        b[top++] = p->count;
        get(p->right);
    }
}

int main()
{
    int m, n;
    while(scanf("%d",&n)!=EOF) {
        trie root;
        init(root);
        for(int i=0;i<n;i++) {
            scanf("%d", &m);
            add(root, m);
        }
        top = 0;
        get(root);
        printf("%d\n", top);
        printf("%d", b[0]);
        for(int i=1;i<top;i++) {
            printf(" %d", b[i]);
        }
        printf("\n");
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值