# Include "stdio. H"
# Include "stdlib. H"
# Include "math. H"
/* Author: Death hunting
Date: 2004.9.22
Description: fair distribution of seats (see mathematical model, Jiang qiyuan)
*/
Void main ()
{
Int I, seat, States, * popular, * seats, sum = 0;
/* Defines the number of states, total number of seats, the population of each State, and the number of seats in each state */
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 )));
/* Apply for memory space for the population and seats of each State */
Printf ("Please intput the total seats:/N ");
Scanf ("% d", & seat );
/* Enter the total number of seats */
Printf ("/nplease input the people of each State:/N ");
For (I = 0; I <states; I ++)
{
Scanf ("% d", popular + I);/* enter the population of each State */
* (Seats + I) = 1;/* The number of seats in each state is initialized to 1 */
Sum + = * (seats + I );
}
Do {
For (I = 0; I <states; I ++)
* (Percent + I) = (float) (* (Popular + I)/(* (seats + I) + 1) * (seats + I )));
/* Relatively unfair calculation */
Max = * (percent + 0 );
For (I = 0; I <states; I ++)
{
If (max <* (percent + I ))
Max = * (percent + I );
/* Find the maximum relative injustice */
}
For (I = 0; I <states; I ++)
{
If (FABS (* (percent + I)-max) <0.00001)
{
* (Seats + I) + = 1;
Sum ++;
/* If the relatively unfair level is the largest, the number of seats is + 1 */
}
}
} While (sum! = Seat );
For (I = 0; I <states; I ++)
Printf ("% 10d", * (seats + I ));
Free (seats );
Free (Popular );
Free (percent );
Printf ("/nwowould you like to use it again? (Y or N)/n ");
Yes = getch ();
} While (Yes = 'y') | (Yes = 'y '));
/* Release space */
Free (Popular );
Free (seats );
Free (percent );
Getch ();
}