Waterloo Cup PREV-32 previous questions Candy (Java)

Source: Internet
Author: User

Time limit of previous questions: 1.0s memory limit: 256.0MB problem description There are N children sitting in a circle. The teacher randomly sends a couple of sweets to each child and then plays the following game:

Each child treats his or her sweets in half to the children on the left hand side.

After a round of sugar, the children with odd sugars were supplied 1 sweets by the teacher, which became even.

Repeat the game until all the children have the same number of sweets.

Your task is to predict how many sweets the teacher will need to reissue in the event of a known initial candy. The input Format program first reads an integer N (2<n<100), indicating the number of children.
followed by a row of n-even numbers separated by spaces (each even no more than 1000, not less than 2) the output format requires that the program output an integer indicating the number of sweets the teacher needs to reissue. Sample Input 3
2 2 4 Sample output 4 ideas are as follows:
Each child divides half candy to the left child, actually is divides all children's candy to divide into half, then own candy number plus the right child's candy number. But one of them is special, the last one of the children's sugar trees should be added to the sugar trees of the first child.

In fact, the first time does not need to determine whether it is odd, because the teacher first distributed all the even.


The code is as follows:

Import Java.util.Scanner;

public class Main {
	static int n;
	Static int[] A;
	public static void Main (string[] args) {
		Scanner Scanner = new Scanner (system.in);
		n = scanner.nextint ();
		A = new Int[n];
		int ans=0;
		for (int i=0;i<n;i++) {
			a[i]=scanner.nextint ();
		}
		while (true) {
			if (is_equal ()) {break
				;
			}
			for (int i=0;i<n;i++) {
				///First all even, no need to Judge
				if (a[i]%2==1) {
					a[i]++;
					ans++;
				}
				a[i]/=2;
			}
			int k=a[0];
			for (int i=0;i<n-1;i++) {
				a[i]+=a[i+1];
			}
			a[n-1]+=k;
		}
		System.out.println (ans);
	public static Boolean is_equal () {to
		(int i=1;i<n;i++) {
			if (A[i]!=a[i-1]) {return
				false;
			}
		} return
		true;
	}



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.