C language: 1 child and a half problems

topic:

  One-and-a-half children, also known as one-girl households with two children, is a family planning policy in some rural areas in mainland China. Couples whose first child is a girl can have a second child. What is the male-to-female ratio if n% of the second child has an artificial sex selection intervention (a boy is selected). (10 points)
Question content:

  One-and-a-half children, also known as one-girl households with two children, is a family planning policy in some rural areas in mainland China. Couples whose first child is a girl can have a second child. If the second child has n% artificial sex selection intervention (boys are selected), what is the ratio of males to females?

Enter n as n%

Input format:

an integer

Output format:

a decimal (2 digits after the decimal point)

Input sample:

5

Sample output:

1.03

Tip: Use a large simulation sample, say 1 million couples.

The output is the total number of boys divided by the total number of girls

printf("%.2lf",double);

 

coding:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain()
{
	int i,boys=0,girls=0;
	int n;
	scanf("%d",&n);
	srand((unsigned)time(NULL));
	for(i=0;i<1000000;i++)
	{
		if(rand()%2==1)
		{
			boys++;
		}else{
			girls++;
			if(rand()%100<n)
			{
				boys++;
			}else{
				if(rand()%2==1)
				{
					boys++;
				}else
				{
					girls++;
				}
			}
		}
	}
	printf("%.2lf",(float)boys/girls);
	return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324785831&siteId=291194637