Fair distribution of seats

Source: Internet
Author: User

# 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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.