选优秀学生干部程序。设有10个候选人,每人每次输入三个的候选人的号,且不得重复计票。投票人数随机,可以弃权,也可以只投一票或二票。要求最后输出每个人得票的结果和前三名的姓名。因为本人是新手,所以希望程序能简单易懂。谢谢
/*
å3åç票æ°:
åé人 1: 14票ã
åé人 2: 10票ã
åé人 8: 8票ã
Press any key to continue
/*
#include <stdio.h>#define N 21 // é举人æ°
int isLegal(int a[3]) { // é票åæ³æ§æ£æ¥
int i;
for(i = 0; i < 3; ++i) {
if(a[i] < 0 || a[0] > 9) // æªæé举人ï¼ä½åº
return 0;
if(a[i] && a[i] == a[(i + 1)%3]) // æéå¤ç¥¨ï¼ä½åº
return 0;
}
return 1; // å ¶ä½çé½æ¯ææ票
}
void sort(int a[],int b[],int n) {
int i,j,k,t;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j)
if(a[k] < a[j]) k = j;
if(k != i) {
t = a[k];
a[k] = a[i];
a[i] = t;
t = b[k];
b[k] = b[i];
b[i] = t;
}
}
}
int main() {
int candidates[11]; // 10ååé人
int electors[N][3] = { // é举人åæ票æ åµï¼0表示ä¸æä»»ä½äººç票
{3,2,1},{1,2,8},{3,2,5},{2,1,8},{3,1,6},{5,3,1},{8,6,9},
{6,8,9},{3,8,9},{5,1,4},{0,1,2},{1,1,2},{3,2,5},{2,2,8},
{5,2,1},{0,0,1},{1,0,2},{8,1,2},{2,1,8},{3,1,4},{8,1,7},
};
int i,j,poll[11] = {0};
for(i = 1; i < 11; ++i) candidates[i] = i;
for(i = 0; i < N; ++i) {
if(isLegal(electors[i])) {
for(j = 0; j < 3; ++j) {
if(electors[i][j]) // 0表示ä¸æä»»ä½äººç票
++poll[electors[i][j]];
}
}
}
sort(poll + 1,candidates + 1,10);
printf("å3åç票æ°:\n");
for(i = 1; i < 4; ++i)
printf("åé人%3d: %d票ã\n",candidates[i],poll[i]);
return 0;
}追é®
å¯ä»¥ç¨æåºç¡çæ¹æ³ååï¼ ææ¯æ°æãããä½ è¿ä¸ªåçè²ä¼¼å¤ªå¤æäºãå®å ¨çä¸æç说
追ç代ç 并ä¸æ²¡æ使ç¨é常ç¨çå
容ï¼å
³é®æ¯ç®æ³ï¼ä»£ç ä¸æå¿
è¦ç注éï¼è¯·ä»ç»é
读ã
å¦å¤ï¼å½æ°isLegal()ä¸ç
if(a[i] 9) // æªæé举人ï¼ä½åº
æ¹ä¸º
if(a[i] 10) // æªæé举人ï¼ä½åº
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-01-02
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define A 10//用于控制投票人数
//候选人的结构体
typedef struct{
char name;//姓名
int num;//票数
}t_man;
//快速排序用的函数
int fun(const void* a,const void* b){
return (*(t_man*)b).num-(*(t_man*)a).num;
}
int main(){
t_man man[10]={0};//定候选人的数量
int work[3]={0};//用于不重复投票
int num = 0;
int a = 0;
int i = 0,j=0;
int flg = 0;//标志位,用于不让重复投一个人
char ch='a';
time_t t;//产生随机种子用
memset(man,0,sizeof(man));//初始化
for(j=0;j<10;j++){//填写候选人的名字
man[j].name=ch++;
}
time(&t);
srand(t);//产生随机种子
while( rand()%A ){//开始投票
memset(work,0,sizeof(work));
i = 0;
a = rand()%4;//投票次数,可以是0,1,2,3 0相当于弃权
while( a-- ){
do{
num = rand()%10;//随机产生1到10的数字,即随机生成把票投给哪个候选人
flg = 0;
for(j=0;j<i;j++){
if(num==work[j])
flg=1;
}
}while( flg );
man[num].num++;
printf("%c的投票数加一\n",man[num].name);
work[i++]=num;
}
printf("本轮投票结束!\n");
}
qsort(man,10,sizeof(t_man),fun);//使用快速排序进行排序
for(j=0;j<10;j++){
printf("%c的得票为%d票\n",man[j].name,man[j].num);
}
printf("得票第一的是%c,为%d票\n",man[0].name,man[0].num);
printf("得票第二的是%c,为%d票\n",man[1].name,man[1].num);
printf("得票第三的是%c,为%d票\n",man[2].name,man[2].num);
return 0;
}
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define A 10//用于控制投票人数
//候选人的结构体
typedef struct{
char name;//姓名
int num;//票数
}t_man;
//快速排序用的函数
int fun(const void* a,const void* b){
return (*(t_man*)b).num-(*(t_man*)a).num;
}
int main(){
t_man man[10]={0};//定候选人的数量
int work[3]={0};//用于不重复投票
int num = 0;
int a = 0;
int i = 0,j=0;
int flg = 0;//标志位,用于不让重复投一个人
char ch='a';
time_t t;//产生随机种子用
memset(man,0,sizeof(man));//初始化
for(j=0;j<10;j++){//填写候选人的名字
man[j].name=ch++;
}
time(&t);
srand(t);//产生随机种子
while( rand()%A ){//开始投票
memset(work,0,sizeof(work));
i = 0;
a = rand()%4;//投票次数,可以是0,1,2,3 0相当于弃权
while( a-- ){
do{
num = rand()%10;//随机产生1到10的数字,即随机生成把票投给哪个候选人
flg = 0;
for(j=0;j<i;j++){
if(num==work[j])
flg=1;
}
}while( flg );
man[num].num++;
printf("%c的投票数加一\n",man[num].name);
work[i++]=num;
}
printf("本轮投票结束!\n");
}
qsort(man,10,sizeof(t_man),fun);//使用快速排序进行排序
for(j=0;j<10;j++){
printf("%c的得票为%d票\n",man[j].name,man[j].num);
}
printf("得票第一的是%c,为%d票\n",man[0].name,man[0].num);
printf("得票第二的是%c,为%d票\n",man[1].name,man[1].num);
printf("得票第三的是%c,为%d票\n",man[2].name,man[2].num);
return 0;
}
第2个回答 2014-01-02
Ubuntu下编写 第一次回答问题,出现了各种问题,同样是初学者,晚了个把小时,十分抱歉!希望共同进步!一起从知道菜鸟走向大神!
#include<stdio.h>
//VOTENUM is the number of man who voteint VOTENUM = 5;
//ten candicates, their ID, name and count of being votedstruct candicate { int ID; char *name; int count;} cand[] = {1, "zhao", 0,2, "qian", 0,3, "sun", 0,4, "li", 0,5, "zhou", 0,6, "wu", 0,7, "zhen", 0,8, "wang", 0,9, "feng", 0,10, "chen", 0};
//If poll0 is equal to zero, this is to say that no one votes for, poll1 is for the fist candicate voted and so on.int vote( int poll0, int poll1, int poll2, int poll3){ if ( poll0 == 0) return 0; while ( 0 < poll1 && poll1 <= 10 && 0 < poll2 && poll2 <= 10 && poll3 <= 10 && 0 < poll3 && poll1 != poll2 && poll1 != poll3 && poll2 != poll3) { cand[poll1-1].count++; cand[poll2-1].count++; cand[poll3-1].count++; return 1; }}
void printCount(){ int i = 0;
for ( ; i < 10; i++) { printf("%s\t%d\n", cand[i].name, cand[i].count); }}
void sort(){ int i = 0, j, k, t; int array[10] = {0};
for ( ; i < 10; i++) { array[i] = cand[i].count; }
for ( i = 0; i < 9; i++) { k = i; for ( j = i+1; j <= 9; j++) { if ( array[j] < array[k]) k = j;
t = array[k]; array[k] = array[i]; array[i] = t; } }
for ( i = 0; i <= 9; i++) { if ( cand[i].count == array[9]) { printf("First\n"); printf("%s\t%d\n", cand[i].name, cand[i].count); }
if ( cand[i].count == array[8]) { printf("Second\n"); printf("%s\t%d\n", cand[i].name, cand[i].count); }
if ( cand[i].count == array[7]) { printf("Third\n"); printf("%s\t%d\n", cand[i].name, cand[i].count); } }}
main(){ printf("%d\n",vote(0,1,2,3)); vote(1,3,5,8); vote(1,4,6,8); vote(1,5,6,8); vote(1,3,4,9);
printCount();
sort();
return 0;}
#include<stdio.h>
//VOTENUM is the number of man who voteint VOTENUM = 5;
//ten candicates, their ID, name and count of being votedstruct candicate { int ID; char *name; int count;} cand[] = {1, "zhao", 0,2, "qian", 0,3, "sun", 0,4, "li", 0,5, "zhou", 0,6, "wu", 0,7, "zhen", 0,8, "wang", 0,9, "feng", 0,10, "chen", 0};
//If poll0 is equal to zero, this is to say that no one votes for, poll1 is for the fist candicate voted and so on.int vote( int poll0, int poll1, int poll2, int poll3){ if ( poll0 == 0) return 0; while ( 0 < poll1 && poll1 <= 10 && 0 < poll2 && poll2 <= 10 && poll3 <= 10 && 0 < poll3 && poll1 != poll2 && poll1 != poll3 && poll2 != poll3) { cand[poll1-1].count++; cand[poll2-1].count++; cand[poll3-1].count++; return 1; }}
void printCount(){ int i = 0;
for ( ; i < 10; i++) { printf("%s\t%d\n", cand[i].name, cand[i].count); }}
void sort(){ int i = 0, j, k, t; int array[10] = {0};
for ( ; i < 10; i++) { array[i] = cand[i].count; }
for ( i = 0; i < 9; i++) { k = i; for ( j = i+1; j <= 9; j++) { if ( array[j] < array[k]) k = j;
t = array[k]; array[k] = array[i]; array[i] = t; } }
for ( i = 0; i <= 9; i++) { if ( cand[i].count == array[9]) { printf("First\n"); printf("%s\t%d\n", cand[i].name, cand[i].count); }
if ( cand[i].count == array[8]) { printf("Second\n"); printf("%s\t%d\n", cand[i].name, cand[i].count); }
if ( cand[i].count == array[7]) { printf("Third\n"); printf("%s\t%d\n", cand[i].name, cand[i].count); } }}
main(){ printf("%d\n",vote(0,1,2,3)); vote(1,3,5,8); vote(1,4,6,8); vote(1,5,6,8); vote(1,3,4,9);
printCount();
sort();
return 0;}