#include "stdio.h"
#include "stdlib.h"
#include "math.h"
/*Author:死神的猎杀
Date:2004.9.22
Description:关于席位的公平分配(见于《数学模型》,姜启源)
*/
void main()
{
int i,seat,states,*popular,*seats,sum=0;
/*定义州数states,总座位数seats,每一个州的人口数,每个州分的座位数*/
float *percent,max;
char yes='y';
do{
printf("Please intput the number of states:/n");
scanf("%d",&states);
popular=(int*)malloc(states*(sizeof(int)));
seats=(int*)malloc(states*(sizeof(int)));
percent=(float*)malloc(states*(sizeof(float)));
/*为每个州的人口数,座位数申请内存空间*/
printf("Please intput the total seats:/n");
scanf("%d",&seat);
/*输入总座位数*/
printf("/nPlease input the people of each state:/n");
for(i=0;i<states;i++)
{
scanf("%d",popular+i);/*输入每个州的人口数*/
*(seats+i)=1; /*每个州的座位数初始化为1*/
sum+=*(seats+i);
}
do{
for(i=0;i<states;i++)
*(percent+i)=(float)(*(popular+i))*(*(popular+i))/((*(seats+i)+1)*(*(seats+i)));
/*计算相对不公平度*/
max=*(percent+0);
for(i=0;i<states;i++)
{
if(max<*(percent+i))
max=*(percent+i);
/*找出最大相对不公平度*/
}
for(i=0;i<states;i++)
{
if(fabs(*(percent+i)-max)<0.00001)
{
*(seats+i)+=1;
sum++;
/*如果相对不公平度最大,分得座位数+1*/
}
}
}while(sum!=seat);
for(i=0;i<states;i++)
printf("%10d",*(seats+i));
free(seats);
free(popular);
free(percent);
printf("/nWould you like to use it again?(Y or N)/n");
yes = getch() ;
}while((yes=='y')||(yes=='Y'));
/*释放空间 */
free( popular ) ;
free( seats ) ;
free( percent ) ;
getch();
}