zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    单例模式
    eclipse部署web项目至本地的tomcat但在webapps中找不到
    使用 google gson 转换Timestamp为JSON字符串
    前端JS对后台传递的timestamp的转换
    2018第15周总结
    Tomcat的最大并发数
    平台对接的另外一种模式
    系统间数据交换的5种方式
    Spring Boot条件注解
    Spring Data JPA 和MyBatis比较
  • 原文地址:https://www.cnblogs.com/cgf1993/p/2915444.html
Copyright © 2011-2022 走看看