Blue Bridge Cup--minute candy

Source: Internet
Author: User

Problem description There are N children sitting around in a circle.  The teacher gave each child a random number of candies and then played the following games: Each child gave his own sweets half to the children on the left hand side.  After a round of sugar, the children with odd sugars were supplied with 1 sweets by the teacher and turned into an even number.  Repeat the game until all the children have the same number of candies. Your task is to predict how many candies a teacher will need to reissue in a known initial candy situation.  The input Format program first reads an integer N (2<n<100), which indicates the number of children. Next is a line of n even numbers separated by a space (each even is not greater than 1000, not less than 2) output format requires the program output an integer, indicating the teacher needs to reissue the number of sweets. Sample input 32 2 4 sample Output 4 It is worth noting that all small pots are carried out at the same time when passing candies. In the process of simulation, you need to use a loop. In order to avoid the next pass, the loop must start with the first one, modify the number of sweets for the first child, modify the second one, and so on. In addition, the number of sweets of the first child must be saved beforehand, so that even after the first child's candy number has changed, the number of candies that should be given to the last child will be known. For example, for example, there are four children, and at the beginning their number of sweets is a,b,c,d. After a round of sweets, their number of candies became: (A+B)/2, (B+C)/2, (C+D)/2, (D+a)/2. Then the teacher gave the odd number of sweets to the children a candy, and so on, until all the children of the same number of sweets, then the end.


#include <stdio.h>
#define MAXN
int A[MAXN];

int main ()
{
	int n,i,count=0;
	scanf ("%d", &n);
	for (i=0;i<n;i++)
	{
		scanf ("%d", &a[i]);
	}
	while (1)
	{
		//check that all children's candy counts are equal for
		(i=1;i<n;i++)
		{
			if (a[i]!=a[0]) break
				;
		}
		if (i==n) break
			;
		Each of the children of their own candy half to the left-hand side of the child
		int num=a[0];
		for (i=0;i<n-1;i++)
		{
			a[i]= (a[i]+a[i+1])/2;
		}
		a[i]= (A[i]+num)/2;
		After a round of sugar, the children with odd sugars were supplied with 1 sweets by the teacher and turned into an even number. For
		(i=0;i<n;i++)
		{
			if (a[i]%2!=0)
			{
				a[i]++;
				count++
	;
	}}} printf ("%d\n", count);
	return 0;
}


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.