小程序:选举投票问题&未知行数输入问题(含代码)
来源:互联网 发布:linux下的常用命令 编辑:程序博客网 时间:2024/05/24 06:45
本程序想记录一下关于 未知行的输入问题。另外注意一下程序中的realloc函数的调用方式,两次栽到这个函数上,也是醉了。。。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description:
小东和其他小朋友正在玩一个关于选举的游戏。选举是通过投票的方式进行的,得票最多的人将获胜。小东是编号为1的候选者,此外还有其他的候选者参加选举。根据初步的调查情况,所有准备投票的小朋友都有一定的投票倾向性,小东如果要获得胜利,必须争取部分准备为其他候选人投票的小朋友。由于小东的资源较为有限,她希望用最小的代价赢得胜利,请你帮忙计算她最少需要争取的选票数
输入有若干组,每组包含两行,第一行为一个正整数n(2<=n<=100),表示候选者的数量,第二行为每个候选人预期得到的选票数,以空格分开,每人的预期得票数在1到1000之间(包含1和1000)。经过小东的争取后,可能出现候选人得票数为0或超过1000的情况。 对每组测试数据,单独输出一行,内容为小东最少需要争取的选票数。
样例:
Input:
5
5 1 11 2 8
4
1 8 8 8
2
7 6output:
4
6
0
/* * 本程序所用的环境为 windows, VC++6.0 * 输入的结束符为 “ctrl+z” 输入结束符后,按回车,如不能输出,再输结束符,按回车即可 */#include<iostream>using namespace std;int Compute_less_score( int * score, int n ); //计算争取票数的函数,其中score 是每个人的可能得票数组,n是人数int Compute_less_score( int * score, int n ){ int score_dong = *( score + 0 ); int count = 1 ; //统计票数大于小东的人数 int score_sum = *( score+0 ); //票数大于小东的总数 for(int i = 1; i< n; i++ ) { if( *( score+0 ) <= * ( score+i ) ) { count += 1; score_sum += * ( score+i ); } } int need_score; if(count == 1) need_score = 0; else need_score = ( score_sum / count + 1 ) - *( score+0 ); //cout<< need_score << endl; return need_score;}int main(){ int n; int count = 0; int init = 1; int * a = ( int *)malloc(sizeof(int ) * init); while( cin>>n ) { int *score = ( int * ) malloc( sizeof(int) * n ); for( int i = 0; i<n; i++ ) { scanf("%d",score + i); } int temp = Compute_less_score( score, n ); if( count >= init ) { a =(int *) realloc( a, sizeof(int)*( init+1 )); init = init + 1; } *( a+count ) = temp; count++; free( score ); } for(int i = 0; i< count; i++ ) cout<< *(a+i) <<endl; free(a); return 0;}
0 0
- 小程序:选举投票问题&未知行数输入问题(含代码)
- C语言 投票选举问题
- 小程序:备考问题&递归实现每天备考时间的分配(含代码)
- 投票小问题
- 选举问题
- 安装程序“发布者:未知 ”问题
- 投票选举
- 村痞了,C++程序小问题(输入流)
- C语言代码行数分类统计小程序(源代码)
- 投票问题
- linux 统计代码行数问题
- 文件上传有关问题(含代码)
- python写的统计PYTHON文件的(代码行数,空行数,注释行数)小程序
- 未知问题
- 投票程序中出现的一些问题。
- 选举/用户投票(不用db)
- 选举问题 状态压缩
- zookeeper选举问题
- OpenSSL-RSA 证书的创建
- Bundle savedInstanceState的作用
- ES6简明教程: 概述与比较
- dot安装
- 在Mac 上配置 React Native环境相关的基础知识
- 小程序:选举投票问题&未知行数输入问题(含代码)
- 关于maven 添加第三方jar包
- 最受欢迎的 FTP 客户端软件
- Servlet API 文档
- Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password
- PL/SQL Developer导入导出数据库方法
- log4j的隔离级别
- 遍历当前文件夹
- 深入理解Activity启动模式