Home » Java » Blue Bridge Cup _ candy test

Title: sub candy

`` has n children sitting in a circle. The teacher randomly sends even candy to each of the children, and then plays the following game:Each child divides half of his candy to the child on the leftAfter a round of sugar, a child with an odd number of sweets is fed by the teacher to 1 candies, thus becoming even numbersPlay the game over and over again until all the children have the same number of sweetsYour task is to predict how many sweets the teacher will need to reissue in the case of the original initial candy``

[format requirements]

The
`` program first reads into an integer N (2Then there is a line of N numbers separated by spaces (each of which is not greater than 1000, no less than 2)Ask the program to output an integer indicating the number of sweets that the teacher needs to reissue``

example: enter

3

224

The program should output:

4

problem solving idea: accept input data, call the method f (), and use recursive method to simulate the process of a round of sugar, and in the case of all the same number of candy, print the results out of recursive.

``Import java.util.Scanner;Public, class, B14_8 {Public, static, Boolean, test (int[], ARR) {Int n = 0;For (int i=1; iIf (arr[i-1]! = arr[i]) {/ / so in i=0; iN=1;Break;}}If (n = = 1) return false;Else return true;}Public, static, void, f (int[], arr, int, count) {If (test (ARR)) {//arr all elements are the sameSystem.out.println (count);Return;}Int[] B = new int[arr.length]; / / cache arrayB[arr.length - 1] = arr[arr.length - 1]/2 + arr/2;For (int i=0; iB[i] = arr[i]/2 + arr[i+1]/2; / / the first half for the left hand side}*B[i-1] = b[i-1] + arr[i]/2;B[i-2]...B = arr + arr/2* /For (int j=0; jIf (b[j]% 2 = = 1) {B[j]++; / / if a replacement is odd.Count++;}Arr[j] = b[j]; / / data transfer}F (arr, count);}Public, static, void, main (String[], args) {//N little friendScanner SC = new Scanner (System.in);Int N = sc.nextInt ();Int[] arr = new int[N];For (int i=0; iArr[i] = sc.nextInt ();}F (arr, 0);}}``