<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-28  評論-51  文章-10  trackbacks-0
    用遞歸法(尾遞歸)求最多元素,即找出出現次數大于n/2的元素
    基于原理:去掉兩個不同的元素,剩下數組里的最多元素仍然是整個數組的最多元素(如果存在的話)

     1 #include <stdio.h>
     2 int majority(int [], int,int );
     3 
     4 int main()
     5 {
     6     int i = 0;
     7     int c = 0;
     8     int data [] = {3,5,5,2,2,2,2};
     9     int maj = majority(data,1 ,7);
    10     for( ; i < 7; i++)
    11     {
    12         if(maj == data[i])
    13            c++;
    14     }    
    15     printf("The majority num of this array is: %d\n", c>3?maj:-1);
    16     return 0;
    17 }
    18 /*s for the begining index, and e for the ending index
    19 * * find the cadidate num of majority*/
    20 int majority(int data[], int s, int e)//begin s = 1, e = n(including)
    21 {
    22     int c = 1;// count the candidate majority num
    23     int j; //fot the index move on
    24     int i = 0;
    25     for( j = s; j< e; j++)
    26     {
    27         if( data[s-1== data[j])
    28             c++;
    29         else
    30         {
    31             if(--c==0)
    32             break;    
    33         }        
    34     }    
    35     if(c>0)
    36         return data[s-1];
    37     else
    38          majority(data, j+1, e);
    39             
    40 }


    posted on 2008-03-29 23:03 fullfocus 閱讀(535) 評論(1)  編輯  收藏 所屬分類: 算法

    評論:
    # re: 求多數元素 2008-09-27 09:53 | 冶人
    品讀了
    Thx!
    如果我想實現求多數元素,但時間復雜度為O(n),其它無限制,該如何實現呢?請教!  回復  更多評論
      
    主站蜘蛛池模板: 免费av一区二区三区| 亚洲精品精华液一区二区| 中文字幕看片在线a免费| 免费一级毛片在级播放| 亚洲日韩在线中文字幕综合| 国产精品免费一级在线观看| 亚洲jizzjizz少妇| 国产免费爽爽视频免费可以看| 国产成人精品久久亚洲高清不卡| 国产小视频在线观看免费| 免费人成网站永久| 亚洲尤码不卡AV麻豆| 日韩免费的视频在线观看香蕉| 亚洲无删减国产精品一区| 亚洲免费人成视频观看| 亚洲综合国产成人丁香五月激情 | 中文字幕久精品免费视频| 久久国产亚洲电影天堂| 国产在线jyzzjyzz免费麻豆| 国产成人亚洲综合一区| 亚洲国产a级视频| 国产自国产自愉自愉免费24区 | 日韩精品在线免费观看| 国产亚洲福利在线视频| 亚洲AV无码乱码精品国产| 永久免费AV无码网站国产| 亚洲专区一路线二| 免费国产在线观看不卡| 国产性生大片免费观看性| 亚洲精品影院久久久久久| 在线免费观看国产视频| 亚洲免费一区二区| 亚洲日韩乱码中文无码蜜桃臀| 在线观看免费精品国产| 免费网站看av片| 亚洲人成色777777精品| 亚洲国产精彩中文乱码AV| 在线免费观看韩国a视频| 性xxxxx大片免费视频| WWW国产亚洲精品久久麻豆| 久久精品国产99精品国产亚洲性色|