Blue Bridge Cup Past Years' Zhenti-Divide Candy

Problem description:
There are n children sitting in a circle. The teacher gave each child an even number of candies at random, and then played the following game:
each child divided his candies into half to the child on his left.
After a round of sugar splitting, the child with an odd number of candies will be given an even number by the teacher.
Repeat this game until all children have the same number of candies.
Your task is to predict how many candies the teacher needs to reissue under the known initial candies.

Input format: The
program first reads an integer N (2< N< 100), which represents the number of children.
Followed by a line of N even numbers separated by spaces (each even number is not greater than 1000, not less than 2)

Output format: The
program is required to output an integer, indicating the number of candies that the teacher needs to reissue.

Sample input:
3
2 2 4

Sample output:
4

c reference code:

#include <stdio.h>

int n;
int a[101];

/*检查小朋友糖果数是否相等*/
int check()
{
    
    
	int i;
	for(i=0;i<n-1;i++)
	{
    
    
		if(a[i]!=a[i+1])
		 return 0;
	}
	
	if(i==n-1)
	 return 1;
}

int main()
{
    
    
	scanf("%d",&n);
	
	int i,sum=0,t;
	for(i=0;i<n;i++)
	 scanf("%d",&a[i]);

    int flag=check();
	while(flag==0)
	{
    
    
	 for(i=0;i<n;i++)
	  a[i]=a[i]/2;//每个小朋友的糖果数减半。
	
	 t=a[0];
	 for(i=0;i<n-1;i++)//每个小朋友都把自己的糖果分一半给左手边的孩子。 
	  a[i]+=a[i+1];
	
	a[n-1]+=t;
	
	for(i=0;i<n;i++)//拥有奇数颗糖的孩子由老师补给1个糖果。
	{
    
    
		if(a[i]%2!=0)
		{
    
    
		 sum++;
		 a[i]+=1;
		}
	}
	
	flag=check();
   }
	
	printf("%d",sum);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/115218739
Recommended