Home » Java » Blue Bridge Cup _ candy test

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 left
After a round of sugar, a child with an odd number of sweets is fed by the teacher to 1 candies, thus becoming even numbers
Play the game over and over again 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 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 same
System.out.println (count);
Return;
}
Int[] B = new int[arr.length]; / / cache array
B[arr.length - 1] = arr[arr.length - 1]/2 + arr[0]/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[0] = arr[0] + arr[1]/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 friend
Scanner 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);
}
}


Latest