Fifth session Waterloo Cup B group java-Candy __java
Source: Internet
Author: User
Package The_five_session_b;
Import Java.util.Scanner;
/*
Title: Cent Candy
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.
"Format Requirements"
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)
Requires the program to output an integer, indicating the number of sweets the teacher needs to reissue.
For example: Enter
3
2 2 4
The program should output:
4
Resource conventions:
Peak memory consumption (including virtual machines) < 256M
CPU Consumption < 1000ms
Please output strictly according to the requirements, do not superfluous print similar: "Please enter ..." Superfluous content.
All the code is placed in the same source file, after debugging, the copy is submitted to the source.
Note: Do not use package statements. Do not use jdk1.7 and the above version of the features.
Note: The name of the main class must be: main, otherwise it will be handled in an invalid code.
*/
public class Fentangguo
{
Record the total number of sweets issued
static int count = 0;
public static void Main (string[] args)
{
Enter the number of children
Scanner S1 = new Scanner (system.in);
int N = S1.nextint ();
Enter the candy that is distributed to each child and put in an array
Scanner s2 = new Scanner (system.in);
Scanner s22= New Scanner (S2.nextline ());
int num[] = new Int[n];
int len = 0;
while (S22.hasnext ())
{
Num[len] = S22.nextint ();
len++;
}
Fun (num);
System.out.println (count);
}
public static void Fun (int num[])
{
Recursive exit, if each element of the modified array is equal, it is eligible to exit.
Boolean flag = true;
for (int i=1; i<num.length; i++)
{
if (Num[i]!=num[i-1])
{
Flag = false;
Break
}
}
if (flag = = True)
Return
int num1[] = new Int[num.length];
Element is divided into half
for (int i=0; i<num1.length; i++)
{
if (i = = 0)
{
Num1[0] = NUM[0]/2+NUM[NUM.LENGTH-1]/2;
}else{
Num1[i] = NUM[I]/2+NUM[I-1]/2;
}
}
Odd element plus 1.
for (int i=0; i<num1.length; i++)
{
if (num1[i]%2!= 0)
{
Num1[i] = num1[i]+1;
count++;
}
}
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.