[Daily Blue Bridge] 37, 2016 provincial competition Java group real question "sub-group"

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, and analyze the algorithm ideas, data structures and other content that exist in it to help you learn To more knowledge and technology!

Title: Sub-group

9 athletes participate in the competition and need to be divided into 3 groups for preliminaries.

What are the grouping schemes?

We stipulate that the athlete is ABCDFGHI

The following program shows all the grouping schemes

The normal output of the program is:

ABC DEF GHI

ABC DEG FHI

ABC DEH FGI

ABC DEI FGH

ABC DFG EHI

ABC DFH EGI

ABC DFI EGH

ABC DGH EFI

ABC DGI EFH

ABC DHI EFG

ABC EFG DHI

ABC EFH DGI

ABC EFI DGH

ABC EGH DFI

ABC EGI DFH

(Omitted below... 560 lines in total)

Read the code carefully and write out the missing content in the underlined part

public class Year2016_Bt4 {

	public static String remain(int []a) {
		String s = "";
		for (int i = 0; i < a.length; i++) {
			if (a[i] == 0) {
				s += (char)(i + 'A');
			}
		}
		return s;
	}
	
	public static void f(String s, int[] a) {
		for (int i = 0; i < a.length; i++) {
			if (a[i]==1) continue;
			a[i]=1;
			for (int j = i+1; j < a.length; j++) {
				if(a[j]==1) continue;
				a[j]=1;
				for (int k = j+1; k < a.length; k++) {
					if(a[k]==1) continue;
					a[k]=1;
					System.out.println(__________________________________);//填空
					a[k]=0;
				}
				a[j]=0;
			}
			a[i]=0;
		}
	}
	
	
	public static void main(String[] args) {
		int [] a = new int[9];		
		a[0] = 1;
		for (int b = 1; b < a.length; b++) {
			a[b] = 1;
			for (int c = b+1; c < a.length; c++) {
				a[c] = 1;
				String s = "A" + (char)(b + 'A') + (char)(c + 'A');
				f(s, a);
				a[c] = 0;
			}
			a[b] = 0;
		}
	}

}

Note: Please do not fill in any existing content or explanatory text.

Problem-solving ideas:

When solving this problem, the most important thing is to read through the source code. In the idea of ​​solving the problem, this problem uses the idea of ​​recursion and backtracking, marking the array elements, and when a certain letter is used, the array position is marked as 1. After this recursion, backtrack the array elements again to prevent the original array content from being disrupted, and then output the determined elements in the form of letters. What should be noted here is the importance of the remain function Don't ignore it, it is the arrangement of the last group of output. Don't think that the function is not used because it is not called in the program! ! !

Answer source code:

public class Year2016_Bt4 {

	public static String remain(int []a) {
		String s = "";
		for (int i = 0; i < a.length; i++) {
			if (a[i] == 0) {
				s += (char)(i + 'A');
			}
		}
		return s;
	}
	
	public static void f(String s, int[] a) {
		for (int i = 0; i < a.length; i++) {
			if (a[i]==1) continue;
			a[i]=1;
			for (int j = i+1; j < a.length; j++) {
				if(a[j]==1) continue;
				a[j]=1;
				for (int k = j+1; k < a.length; k++) {
					if(a[k]==1) continue;
					a[k]=1;
					System.out.println(s + " " + (char)(i + 'A') + (char)(j + 'A') + (char)(k + 'A') + " "  + remain(a));//填空
					a[k]=0;
				}
				a[j]=0;
			}
			a[i]=0;
		}
	}
	
	
	public static void main(String[] args) {
		int [] a = new int[9];		
		a[0] = 1;
		for (int b = 1; b < a.length; b++) {
			a[b] = 1;
			for (int c = b+1; c < a.length; c++) {
				a[c] = 1;
				String s = "A" + (char)(b + 'A') + (char)(c + 'A');
				f(s, a);
				a[c] = 0;
			}
			a[b] = 0;
		}
	}

}

 

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/115030886