01 Java Basic Grammar-Section 5 Array-Captain Selection Game Training

1. The task of selecting captain game training

Problem Description

Today, the students met to climb the mountain and play together. In order to better carry out this activity, everyone is going to nominate someone as the temporary captain of the trip. In order to reflect reasonableness and fairness, everyone proposed a more interesting rule. All people form a circle, arranged in order . Starting from the first person to report the number (from 1 to 3), all those who report to 3 exit the circle, the remaining people continue to report the number , and the last one remaining is elected as the captain. You through the preparation process, find a group of people is the captain had the first of several students.

Problem solving ideas

Analyzing the meaning of the question, you need to exit the circle if the number is 3, and continue to report the number. Considering that the length of the array is fixed, and you want to withdraw elements from the array (how to shorten the length of the array), then useSystem.arraycopy()method. But the question is, how do you determine the original position of the captain if you use this method to change the length of the array? (unsolved puzzle)

Analysis of System.arraycopy();

We all know that the array is a reference data type, and the length of the array is fixed, so how to shorten/increase the length of the array (exit the elements from the array)? ——System.arraycopy();

System provides a static method arraycopy(), we can use it to copy between arrays, so as to shorten/increase the length of the array.

截断一个数组,中间舍弃连续的数字,就要复制两次。

The function prototype is:

public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

which is:System.arraycopy(原数组, 从原数据的起始位置开始【下标】, 目标数组, 目标数组的开始起始位置【下标】, 要copy数组的长度【不下标】);

//测试复制
public static void main(String[] args) {
    
    
		
		int[] a = {
    
    3,1,8,7,6,2,9};
		int[] b = new int[10];
		//3 1 8 7 6 2 9
		//0 1 2 3 4 5 6 下标
		System.arraycopy(a, 1, b, 4, 3);//将a数组下标为1的数据 长度3 复制到b数组下标为4的地方
		//a复制到b
		//b数组遍历
		for(int i = 0; i < b.length; i ++) {
    
    
			System.out.print(b[i] + " ");
		}
		System.out.println();
	}

operation result:
Insert picture description here

//测试去掉数组其中的数据
public static void main(String[] args) {
    
    
		
		int[] a = {
    
    3,1,8,7,6,2,9};
		int[] b = new int[6];
		//将a数组中的8去掉,后面的数据向前移,数组长度-1
		//3 1 8 7 6 2 9
		//0 1 2 3 4 5 6 下标
		//截断一个数组,中间舍弃一个数字,就要复制两次
		int index = 2;//要舍弃掉的哪个数字的下标
		System.arraycopy(a,0,b,0,index);
		System.arraycopy(a,index+1,b,index,a.length-index-1);
		//a复制到b
		//b数组遍历
		for(int i = 0; i < b.length; i ++) {
    
    
			System.out.print(b[i] + " ");
		}
		System.out.println();
	}
}

Operation result:
Insert picture description here
In order to make the code flexible and convenient, we can encapsulate the code , as shown in the following code:

public static void main(String[] args) {
    
    
		
		int[] a = {
    
    3,1,8,7,6,2,9};
		int[] b = new int[6];
		//将a数组中的8去掉,后面的数据向前移,数组长度-1
		//3 1 8 7 6 2 9
		//0 1 2 3 4 5 6 下标
		//截断一个数组,中间舍弃一个数字,就要复制两次
		int index = 2;//要舍弃掉的哪个数字的下标
		a = kickOff(a,index);
		//a复制到b
		//b数组遍历
		showArray(a);
	}
	/**
	 * 将指定数组中的一个指定位置的元素,后面的元素前移
	 * @param a
	 * @param index
	 * @return
	 */
	private static int[] kickOff(int[] a,int index) {
    
    
		int[] b = new int[a.length - 1];
		System.arraycopy(a,0,b,0,index);
		System.arraycopy(a,index+1,b,index,a.length - index -1);
		return b;
	}
	/**
	 * 显示一个int数组
	 * @param a
	 */
	private static void showArray(int[] a) {
    
    
		System.out.println(Arrays.toString(a));
	}
}

operation result:
Insert picture description here

Code

//该代码只能求出队长的数据,而不能获得队长原先的下标位置(未解之谜)
import java.util.Arrays;
import java.util.Scanner;

public class Sys {
    
    

	public static void main(String[] args) {
    
    
		
		System.out.println("请输入一个初始的队伍长度:");
		Scanner input = new Scanner(System.in);
		int len = input.nextInt();
		int[] nums = new int[len];
		System.out.println("初始:");
		for(int i = 0; i < nums.length; i ++) {
    
    
			nums[i] = i + 1;
		}
		showArray(nums);

		int count = 0;//当前剩余的人数定义变量存放数字3的总人数,目的判断是否为剩下的最后一位队员,若是,则为队长。
		int round = 1;
		int i = 0;
		int a = 1;//报数起始数字为1
		int b = 0;
		
		while(nums.length > 1) {
    
    
			System.out.println("第"+(round++) + "轮:");
			for(i = 0; i < nums.length; i ++) {
    
    
				//报数
				if(a == 3) {
    
    
					nums = kickOff(nums,i);
					showArray(nums);
					count ++;
					i--;
				}
				a++;
				if(a == 4) {
    
    
					a = 1;
				}
			}
		}
	}
	
	/**
	 * 将指定数组中的一个指定位置的元素,后面的元素前移
	 * @param a
	 * @param index
	 * @return
	 */
	private static int[] kickOff(int[] a,int index) {
    
    
		int[] b = new int[a.length - 1];
		System.arraycopy(a,0,b,0,index);
		System.arraycopy(a,index+1,b,index,a.length - index -1);
		return b;
	}
	/**
	 * 显示一个int数组
	 * @param a
	 */
	private static void showArray(int[] nums) {
    
    
		System.out.println(Arrays.toString(nums));
	}
}

operation result:
Insert picture description here

//该代码是通过比较被淘汰人数确定最后一位是队长,进而确定队长原先的位置(始终都没改变数组的长度、位置)
import java.util.Scanner;

public class Test_1_1 {
    
    

	public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
		System.out.println("请您输入成员数目: ");
		int n = input.nextInt();//确定数组长度
		// 1.定义一个数组存放n个人
		int[] nums = new int[n];
		//对数组动态赋值
		for(int i = 0; i < nums.length; i ++) {
    
    
			nums[i] = 1;//给数组都初始化为1
		}
		// 2.定义一个变量,存放报数的数字 1 2 3,循环报数
		int a = 1;
		//当前剩余的人数定义变量存放数字3的总人数,目的判断是否为剩下的最后一位队员,若是,则为队长。
		int count = 0;
		//存放队长编号(最后一个pass的人)
		int m = 0;
	
			while(count < n-1) {
    
    
				for(int i = 0; i < n; i++) {
    
    
					if(nums[i] != 3) {
    
    			// 跳过报数为3的人(已被淘汰)
						if(a > 3) {
    
    			
							a = 1;			// 重新开始报数
						}
						nums[i] = a++;		// 记录每个队员所报的数字 并且更新下一个要报的数
						if(nums[i] == 3) {
    
    
							count++;				// 当前队列中报数为3的总人数
						}else {
    
    
							m = i;				// 更新最后一个淘汰的人
						}
					}
				}
			}
			System.out.println("确定了这次游玩的队长,其原下标为:" + m);
	}
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46312449/article/details/112506712