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.
Input format
The program first reads an integer N (2<n<100), indicating the number of children.
followed by a row of n-even numbers separated by a space (every even number is no greater than 1000, not less than 2)
Output format
Requires the program to output an integer, indicating the number of sweets the teacher needs to reissue.
Sample input
3
2 2 4
Sample output
4
public class Main {static int addsweet=0;
Initialize the number of candies to be reissued public static void Main (string[] args) {Scanner input=new Scanner (system.in);
Get the number of students int student=input.nextint ();
Define the number of sweets for each person array int[] sweet=new int[student];
for (int i = 0; i < sweet.length i++) {sweet[i]=input.nextint ();
}//Definition of the candy array int[] temp=new int[student];
While loop while (true) {//Execute Candy Change method Chang (sweet,temp);
Perform checks for the same if (check (sweet)) {System.out.println (addsweet);
Return
//Perform the operation of the replacement candy, pass the number of replacement candy, return whether to reissue, judged as no replacement break cycle add (sweet);
Perform checks for the same if (check (sweet)) {System.out.println (addsweet);
Return }}/** * Check if candy is the same * @param sweet * * public static Boolean check (int[] sweet) {//TODO auto-generated Meth Od stub boolean flag=true; Initialization judgments are the same for (int i = 0; i < sweet.length i++) {for (int j = 0; J < Sweet.length; J +) {if sweet[i]!=s
WEET[J]) {//produced a different flag=false; }} return FLag
/** * Change the number of sweets * @param sweet * @param temp/public static void Chang (int[] sweet, int[] temp) {//array each divided by 2
for (int i = 0; i < sweet.length; i++) {SWEET[I]=SWEET[I]/2;
Traversal is stored into the temp array temp[i]=sweet[i];
for (int i = 0; i < sweet.length i++) {if (i==0) {//When the last one takes sweet[i]=sweet[i]+temp[temp.length-1 with the first one]; }else{//This is the selected event Sweet[i]=sweet[i]+temp[i-1];//Add Candy}}/** * Add odd candy * @param sweet * @pa Ram AddSweet2 * @return */public static void Add (int[] sweet) {//circular judgment, if it is odd, add a candy, adding count for (int i = 0; i < Sweet.length;
i++) {if (sweet[i]%2!=0) {sweet[i]++;
addsweet++;
}
}
}
}