HLG 数字去重和排序II【二叉排序树】
| Description |
| 用计算机随机生成了N个0到1000000000(包含0和1000000000)之间的随机整数(N≤5000000),对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再把这些数从小到大排序。 请你完成“去重”与“排序”的工作 |
| Input |
| 输入有2行,第1行为1个正整数,表示所生成的随机数的个数: N 第2行有N个用空格隔开的正整数,为所产生的随机数。 |
| Output |
| 输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。 |
| Sample Input |
| 10 20 40 32 67 40 20 89 300 400 15 |
| Sample Output |
|
8 |
code:
View Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int b[5000001];
int 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 i,m,n;
while(scanf("%d",&n)!=EOF)
{
trie root;
init(root);
for(i=0;i<n;i++)
{
scanf("%d",&m);
add(root,m);
}
top=0;
get(root);
printf("%d\n",top);
printf("%d",b[0]);
for(i=1;i<top;i++)
printf(" %d",b[i]);
printf("\n");
}
return 0;
}



浙公网安备 33010602011771号