测试选举,并打印最终谁获胜

本文展示了一个使用C语言编写的简单程序,该程序能够处理一个二维数组中的选票数据,实现统计每行的选票总数、筛选得票超过3票的候选人、将这些候选人的选票清零并重新计算总票数,最终确定胜出者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "stdio.h"

#define MaxRow 11 
#define MaxCol 7 

int main()
{
 int i,j,c,q,k = 0;
 int aa[MaxRow][MaxCol] = {{1,0,0,0,1,0},{1,1,1,0,0,0,},{0,0,0,0,1,1},{1,1,1,1,1,1},{0,0,1,1,1},{1,1,1,0,1,0},{1,0,0,0,1,1},{1,1,1,0,0,1},{0,0,0,0,1,1},{1,1,1,1,1,0}};
 int p[MaxCol];
 //Print the numbers of the array of aa
 for(i = 0;i < MaxRow;i ++)
 {
  for(j = 0;j < MaxCol;j ++)
  {
   printf("%3d",aa[i][j]);
  }
  printf("/n");
 }
 //the sum of every row
 for(i = 0;i < MaxRow-1;i ++)
 {
  for(j = 0;j < MaxCol-1;j ++)
  {
   aa[i][MaxCol-1] += aa[i][j];
  }
 }
 printf("/n/n");
 //print the numbefs of the array of aa
 for(i = 0;i < MaxRow;i ++)
 {
  for(j = 0;j < MaxCol;j ++)
  {
   printf("%3d",aa[i][j]);
  }
  printf("/n");
 }
 //find the number of  votes whose voters > 3 and put in the array of p
 for(i = 0;i < MaxRow-1;i ++)
 {
  if(aa[i][MaxCol-1] > 3)
  {
   p[k++] = i;
  }
 }
 //put the votes of the every element who the voters >3  to  0
 //why ?,please consider carefully
 for(i = 0;i < MaxRow;i ++)
 {
  for(j = 0;j < MaxCol;j ++)
  {
   for(q = 0;q < k;q ++)
   {
    if(i == p[q])
    {
     aa[i][j] = 0;
    }
   }
  }
 }
 //the sum of every col
 for(i = 0;i < MaxRow-1;i ++)
 {
  for(j = 0;j< MaxCol-1;j ++)
  {
   aa[MaxRow-1][j] += aa[i][j];
  }
 }
 printf("/n/n");
 //print the whole of the array of aa
 for(i = 0;i < MaxRow;i ++)
 {
  for(j = 0;j < MaxCol;j ++)
  {
   printf("%3d",aa[i][j]);
  }
  printf("/n");
 }
 //choice who win
 int max = aa[MaxRow-1][0];
 for(i = 1;i < MaxCol - 1;i ++)
 {
  if(max < aa[MaxRow-1][i])
  {
   max = aa[MaxRow-1][i];
   c = i;
  }
 }
 printf("/n第 %d 位候选人获胜,他获得的票数为:%d/n/n",c+1,max);

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值