中国人的选举问题

解决这个问题不能排序.人太多了.

假设在给定的投票结果中任选两张,一张投给A,一张投给B.我们可以把这两张票都去了.因为:

1.如果A与B都没有得到过半数的票,那么去了A,B也不会影响真正超过半数的那个人的当选.

2如果A与B有一个人得票过半数,假设是A那么A得得票为N/2+k那么去了这两张票,A得票为N/2+k-1剩下的票为N-2

则N/2+k-1>(N-2)/2即得票还是超过一半.

根据这个思想,可以写出如下程序.

 

 

/* ------------------------------------------------------ */
/* FUNCTION majority :                                    */
/*    Given vote[] containing a voting record, say x[i]=j */
/* meaning that person i votes person j, this function    */
/* determine if there is any one is a majority.  There is */
/* no limit to the value of j.  In other words, anybody   */
/* can vote someone else and there is no fixed candidate. */
/* Thus value in x[] could be very large, or even larger  */
/* than the size of x[].  'Majority' means that there is  */
/* a guy such that more than n/2 persons votes for him.   */
/*                                                        */
/* Copyright Ching-Kuang Shene               July/01/1989 */
/* ------------------------------------------------------ */

int majority(int vote[], int n)
{
     int  candidate;          /* current candidate        */
     int  vote_count;         /* vote count for the cand. */
     int  count;              /* final count              */
     int  answer;             /* final answer             */
     int  i;

     candidate  = vote[0];    /* assume the guy voted by  */
     vote_count = 1;          /* #1 is the candidate      */
     for (i = 1; i < n; i++)  /* for all other people ... */
          if (vote_count == 0) {  /* if current no count  */
               candidate  = vote[i]; /* pick up a new can.*/
               vote_count = 1; /* give him count 1        */
          }
          else if (candidate == vote[i]) /* is he a candi?*/
               vote_count++;  /* increase his count       */
          else
               vote_count--;  /* NO, decrease his count   */

     if (vote_count == 0)     /* finally check if the cand*/
          answer = 0;       /* is the guy with majority */
     else {
          for (i = 0, count = 0; i < n; i++)
               if (vote[i] == candidate)
                    count++;
          answer = (count > (int)(n / 2.0 + 0.5)) ? candidate : 0;
     }
     return answer;
}


/* ------------------------------------------------------ */

#include  <stdio.h>

void main(void)
{
     int  x[] = { 2, 2, 4, 2, 1, 2, 5, 2, 2, 8 };
     int  n = sizeof(x)/sizeof(int);
     int  answer, i;

     printf("/nMajority Counting Program");
     printf("/n=========================");
     printf("/n/n  No   Vote");
     printf(  "/n  --   ----");
     for (i = 0; i < n; i++)
          printf("/n%4d%6d", i, x[i]);

     answer = majority(x, n);

     printf("/n");
     if (answer > 0)
          printf("/nMajority is %d", answer);
     else
          printf("/nThere is no majority");
}
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值