[] Blue Bridge Cup points candy (violence title)

Problem Description

There are n children sitting in a circle. The teacher gave each child an even number of random hair candy, then the following games:

Each child shows his candy to the left-hand half of the children.

After a minute of sugar, with odd sweets child by the teacher supply a candy, to become even.

This game is repeated until the number of candy all children are the same so far.

Your task is to predict in the initial situation known candy, candy teacher total number of required replacement.
Input
program first read an integer N (2 <N <100) , represents the number of children.
Line separated by a space followed by the N even (each even not greater than 1000, not less than 2)
output
in claim program outputs an integer representing the number of candy teacher needs replacement.
Sample input
. 3
2 2 4
Sample Output
4

analysis

At first the wrong question ...... I thought finally asked each child is an even number, the teacher is no longer necessary to candy, like a long, long time, the result has been wa
personally think there is no problem, then look back problems and found that the trial was wrong moderation
Finally, violence can solve

package 分糖果;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n;
		int sum=0;
		int flag=1;
		int []x=new int[105];
		n=in.nextInt();
		for(int i=1;i<=n;i++)
		{
			x[i]=in.nextInt();
		}
		while(flag==1)
		{
			flag=0;
			int k=x[1]/2;
			for(int i=1;i<n;i++)
			{
				x[i]=x[i]/2+x[i+1]/2;
				if(x[i]%2==1)
				{
					x[i]=x[i]+1;
					sum+=1;
				}
				//System.out.println(x[i]);
			}
			x[n]=x[n]/2+k;
			if(x[n]%2==1)
			{
				x[n]=x[n]+1;
				sum+=1;
			}
			//System.out.println(x[n]);
			int t=x[1];
			for(int i=2;i<=n;i++)
			{
				if(x[i]!=t)
				{
					flag=1;
				}
			}
		}
		System.out.println(sum);
		
	}

}

Published 20 original articles · won praise 0 · Views 3867

Guess you like

Origin blog.csdn.net/weixin_44544406/article/details/104203336