Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535510
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2016-03-02 16:16:01

数组数字超过了一半,意味着这个数字出现的次数比其它数字的总和还要多。假设所有数字中不相等的可以两两抵消,那最后剩下来的肯定是那个超过一半的数字。利用这个思路,先记录第一个数组元素为a,并记录出现次数为1,如何接下来的数组元素和a相同,则num++,如果不相同,则num--,如果num ==0 时,则重新记录新的数组元素,并初始化num为1. 如果存在超过一半的数字,那么这个数字必定是a,但是有一种情况,如数组为{1,2,3},那么1,2相互抵消,剩下a=3,num=1,但是3并不是那个超过一半的数字,所以还必须重新循环判断a存在的个数是否超过了数组的一半长度。如果是,则记录输出。
这里的isvalid是用来标记是否是有超过一半的数字存在,在入参错误或者没有的情况下,这个字为false。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define bool int
  4. #define false 0
  5. #define true 1

  6. bool isvalid = false;
  7. int MoreThanHalfNum(int* numbers, int length)
  8. {
  9.     int i;
  10.     int a,num;
  11.     if(numbers == NULL || length <= 0) {
  12.         return 0;
  13.     }
  14.     a = numbers[0];
  15.     num = 1;
  16.     for (i = 1; i < length; i++) {
  17.         if(num == 0) {
  18.             a = numbers[i];
  19.             num=1;
  20.         }
  21.         else if(a == numbers[i]) {
  22.             num++;
  23.         }else {
  24.             num--;
  25.         }
  26.     }
  27.     num = 0;
  28.     for(i = 0; i < length; i++) {
  29.         if(numbers[i] == a)
  30.             num++;
  31.     }
  32.     if(num > length/2) {
  33.         isvalid = true;
  34.         return a;
  35.     }
  36.     return 0;
  37.     /*this judge is false when a[] = {1,3,2}
  38.      * if(num >= 1) {
  39.         isvalid = true;
  40.         return a;
  41.     }else
  42.         return 0;
  43.     */
  44. }
  45. int main(int argc, char*argv[])
  46. {
  47.     int a1[] = {2,2,1,1,2};
  48.     int a2[] = {1,2,2,1,1};
  49.     int a3[] = {1,1,2,2};
  50.     int a4[] = {1,3,2};
  51.     int result;
  52.     result = MoreThanHalfNum(a1,sizeof(a1)/sizeof(int));
  53.     if(isvalid)
  54.         printf("more than half num of a1 is %d\n",result);
  55.     else
  56.         printf("no more than half num in the array a1\n");
  57.     
  58.     isvalid = false;
  59.     result = MoreThanHalfNum(a2,sizeof(a2)/sizeof(int));
  60.     if(isvalid)
  61.         printf("more than half num of a2 is %d\n",result);
  62.     else
  63.         printf("no more than half num in the array a2\n");
  64.     
  65.     isvalid = false;
  66.     result = MoreThanHalfNum(a3,sizeof(a3)/sizeof(int));
  67.     if(isvalid)
  68.         printf("more than half num of a3 is %d\n",result);
  69.     else
  70.         printf("no more than half num in the array a3\n");

  71.     isvalid = false;
  72.     result = MoreThanHalfNum(a4,sizeof(a4)/sizeof(int));
  73.     if(isvalid)
  74.         printf("more than half num of a4 is %d\n",result);
  75.     else
  76.         printf("no more than half num in the array a4\n");

  77.     return 0;
  78. }
运行结果

点击(此处)折叠或打开

  1. more than half num of a1 is 2
  2. more than half num of a2 is 1
  3. no more than half num in the array a3
  4. no more than half num in the array a4

阅读(762) | 评论(0) | 转发(0) |
0

上一篇:git命令与github使用

下一篇:Aho-Corasick算法

给主人留下些什么吧!~~