[Daily Blue Bridge] 24. Four years of provincial competition Java group real question "Split Candy"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Sub-Candy

There are n children sitting in a circle. The teacher gives each child an even number of candies at random, and then plays the following game:

Each child gave half of his candy to the child on the left,

After a round of sugar splitting, the child with an odd number of candies will be given an even number by the teacher.

Repeat this game until all the children have the same number of candies.

Your task is to predict how many candies the teacher needs to reissue under the known initial candies.

【format requirement】

The program first reads an integer N (2<N<100), which represents the number of children,

By a line of N even numbers separated by spaces (each even number is not greater than 1000, not less than 2)

The program is required to output an integer, indicating the number of candies that the teacher needs to reissue.

For example: enter

3

2 2 4

The program should output:

4

Resource agreement:

Peak memory consumption (including virtual machines) <256M

CPU consumption <1000ms

Please output in strict accordance with the requirements, and do not print superfluous content like "please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.

Note: Do not use package statement, do not use jdk1.6 and above version features

Note: The name of the main class must be Main, otherwise it will be treated as an invalid code.

Problem-solving ideas:

The idea in this question is quite clear. We can store the number of candies owned by each child in an array, and then we can use a while loop or recursion to play each round of the game. After each round of the game is over, Determine whether the number of elements in the array is equal.

Next, use while loop and recursion to solve this problem separately:

Answer source code:

while loop method

package 一四年省赛真题;

import java.util.Scanner;

public class Year2014_Bt8_2 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int[] arr = new int[N];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = scanner.nextInt();
		}
		int ans = 0;
		while (true) {
			int firstN = arr[0];
			for (int i = 0; i < arr.length; i++) {
				if (i==arr.length-1) {
					arr[i] = arr[i]/2 + firstN/2;
				} else {
					arr[i] = arr[i]/2 + arr[i+1]/2;	
				}
			}
			//老师补发糖果
			for (int i = 0; i < arr.length; i++) {
				//如果当前手中的糖果数量是奇数个
				if (arr[i]%2!=0) {
					arr[i]+=1;
					ans++;
				}
			}
			//判断数组中的元素是否都相等
			if (check(arr)) {
				System.out.println(ans);
			}
			
		}

	}

	//判断数组元素值是否都相等
	private static boolean check(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]!=arr[0]) {
				return false;
			}
		}
		return true;
	}
	}

}

Recursion

package 一四年省赛真题;

import java.util.Scanner;

public class Year2014_Bt8 {
	static int ans = 0;	//记录补发的糖果数
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int[] arr = new int[N];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = scanner.nextInt();
		}
		f(arr);
		System.out.println(ans);

	}

	//每一轮游戏
	private static void f(int[] arr) {
		
		int firstN = arr[0];	//记录第一个数组元素
		//利用循环将每一个小朋友手中的糖果给左边的小朋友分一半
		for (int i = 0; i < arr.length; i++) {
			if (i==arr.length-1) {
				arr[i] = arr[i]/2 + firstN/2;
			}else {
				arr[i] = arr[i]/2 + arr[i+1]/2;
			}		
		}
		//老师补发糖果
		for (int i = 0; i < arr.length; i++) {
			//如果当前手中的糖果数量是奇数个
			if (arr[i]%2!=0) {
				arr[i]+=1;
				ans+=1;
			}
		}
		//判断数组中的元素是否都相等
		if (check(arr)) {
			return;
		}
		//进行下一轮游戏
		f(arr);
		
	}

	//判断数组元素值是否都相等
	private static boolean check(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]!=arr[0]) {
				return false;
			}
		}
		return true;
	}

}

 

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/113727649