问题描述:
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。
Input Param
n 输入随机数的个数
inputArray n个随机整数组成的数组
Return Value
OutputArray 输出处理后的随机整数
参考答案:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define LEN sizeof(struct node) 4 5 struct node 6 { 7 int value; 8 struct node * pleft; 9 struct node * pright; 10 }; 11 struct node * head = (struct node *)malloc(LEN); 12 13 void insertTree(int temp, struct node * p) 14 { 15 if(p->value == 0) 16 { 17 p->value = temp; 18 } 19 else if(temp>p->value) 20 { 21 if(p->pright == NULL) 22 { 23 p->pright = (struct node *)malloc(LEN); 24 p->pright->value = 0; 25 p->pright->pleft = NULL; 26 p->pright->pright = NULL; 27 } 28 insertTree(temp, p->pright); 29 } 30 else if(temp<p->value) 31 { 32 if(p->pleft == NULL) 33 { 34 p->pleft = (struct node *)malloc(LEN); 35 p->pleft->value = 0; 36 p->pleft->pleft = NULL; 37 p->pleft->pright = NULL; 38 } 39 insertTree(temp, p->pleft); 40 } 41 else if(temp == p->value) 42 return; 43 } 44 45 void printTree(struct node * p) 46 { 47 if(p->pleft!=NULL) printTree(p->pleft); 48 if(p!=NULL) printf("%d\n", p->value); 49 if(p->pright!=NULL) printTree(p->pright); 50 } 51 52 int main(void) 53 { 54 int n; 55 56 int temp; 57 58 //scanf("%d", &n); 59 while(scanf("%d",&n)!=EOF) 60 { 61 scanf("%d", &temp); 62 head->value = temp; 63 head->pleft = NULL; 64 head->pright = NULL; 65 for(int i=1; i<n; i++) 66 { 67 scanf("%d", &temp); 68 insertTree(temp, head); 69 } 70 printTree(head); 71 } 72 73 return 0; 74 }