|
#include <stdio.h>
int bestman(int h[4]) { int i,j,temp,best,m[4]; for(i=0;i<=3;i++) m[i]=h[i]; for(i=0;i<3;i++) for(j=2;j>=0;j--) if(m[j+1]>m[i]) {temp=m[j+1];m[j+1]=m[i];m[i]=temp;} return(m[0]); }
main() { int man[4]={0,0,0,0},i,max; char push; do{ printf("Please input the best candidate which you wish\n"); printf("1.JiangZeMing\n2.MaoZeDong\n3.DengXiaoPing\n4.ChenShuiBian\n"); push=getch(); for(i=0;i<=5;i++) printf("\n"); switch(push){ case '1':man[0]++;break; case '2':man[1]++;break; case '3':man[2]++;break; case '4':man[3]++;break; case '0':break; default:printf("\nPlease input 1 to 4\n"); } }while(push!='0'); max=bestman(man); for(i=0;i<=3;i++) if(man[i]==max) break; printf("The best candidate is:"); switch(i){ case 0:printf("JiangZeMin");break; case 1:printf("MaoZeDong");break; case 2:printf("DengXiaoPing");break; case 3:printf("ChenShuiBian");break; } printf(" %d tickets\n",max); getch(); }
以上是我在网吧随即之作,没有很好的思路(我旁边是个MM),采用了很多笨方法,请不要见笑
声明,本程序所有人名均为乱编,如有雷同纯属巧合。本程序和政治无关~谢谢
[此贴子已经被作者于2004-07-22 23:45:50编辑过]
和这个函数差不多
改一下就行
# include "stdio.h" # define RESPONSE_SIZE 40 //宏定义 # define FREQUENCY_SIZE 11
main() { int answer; //变量声明 int rating; //变量声明 int responses[RESPONSE_SIZE]={1,2,6,4,8,5,9,7,8, 10,1,6,3,8,6,10,3,8,2,7,6,5,7,6,8,6,7, 5,6,6,5,6,7,5,6,4,8,6,8,10}; //声明并初始化数组 int frequency[FREQUENCY_SIZE]={0}; //将数组初始化为0 //for结构 for(answer=0;answer<=RESPONSE_SIZE-1;answer++) //如果answer不大于RESPONSE_SIZE-1,answer加一 { ++frequency[responses[answer]]; //对应的概率加一 }//end for printf("%s%17s\n","Rating","Frequency"); //打印信息 //for结构 for(rating=1;rating<=FREQUENCY_SIZE-1;rating++) //如果rating不大于FREQUENCY_SIZE-1,rating加一 { printf("%6d%17d\n",rating,frequency[rating]);//打印结果 }//end for
return 0; //返回0,表明程序成功的结束 }//end main
/*输出结果 Rating Frequency 1 2 2 2 3 2 4 2 5 5 6 11 7 5 8 7 9 1 10 3 */