NEU1141the unique number

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int a[100010];
 4 int cmp(const void *a,const void *b)
 5 {
 6     return *((int*)a)>*((int*)b)?1:-1;
 7 }
 8 void choose_num(int a[],int pp)//先排序 后查找.....
 9 {
10     int i,j,count=1;
11     for(i=0;i<pp-1;i++)
12     {
13         if(a[i+1]==a[i])
14             count++;
15         else
16         {
17             if(count%2==1)
18               { printf("%d\n",a[i]); return;}
19             count=1;
20         }
21     }
22     if(count==1) { printf("%d\n",a[i]); return;}
23 }
24 int main()
25 {
26    /* freopen("input.txt","r",stdin);
27     freopen("output.txt","w",stdout);*/
28 
29     int pp;
30     while(scanf("%d",&pp)!=EOF)
31     {
32     int i;
33     for(i=0;i<pp;i++) scanf("%d",&a[i]);
34     qsort(a,pp,sizeof(a[0]),cmp);
35     choose_num(a,pp);
36 
37     }
38     return 0;
39 }

虽然也是一道水题,但自己还是稍微比较高兴的...

把这个代码贴上来就是让自己能比较一下时间复杂度,  一开始的思路就是那样一个个找,两次循环,结果超时.....

                                                                       第二个思路是先快速排序然后从a[0]开始依次往后找..... 时间还行.....

                                                                      以后得尝试Try O(n) algorithm.

posted @ 2013-02-18 12:09  闭关修炼的小孩纸  阅读(166)  评论(0编辑  收藏  举报