Sub group 9 Athletes need to be divided into three groups for preliminary competition. //What are the grouping schemes? //We mark athletes as A,B,C,... I //The following program lists all the grouping methods. //The normal output of this 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 ABC EHI DFG ABC FGH DEI ABC FGI DEH ABC FHI DEG ABC GHI DEF ABD CEF GHI ABD CEG FHI ABD CEH FGI ABD CEI FGH ABD CFG EHI ABD CFH EGI ABD CFI EGH ABD CGH EFI ABD CGI EFH ABD CHI EFG ABD EFG CHI ..... (The following is omitted, 560 lines in total).
public class A { 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(__________________________________); //Fill in position 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; } } }
public class Main { 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)('A'+i)+(char)('A'+j)+(char)('A'+k)+" "+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; } } }
draw Planet X will send a five person observer mission to planet W. Among them: Country A can send up to four people. Country B can send up to two people. Country C can send up to two people. .... So how many different combinations of countries will the final mission to W star have? The following procedure solves this problem. Array a [] is the largest number of places that each country can send. The execution result of the program is: DEFFF CEFFF CDFFF CDEFF CCFFF CCEFF CCDFF CCDEF BEFFF BDFFF BDEFF BCFFF BCEFF BCDFF BCDEF .... (omitted below, 101 lines in total)
public class A { public static void f(int[] a, int k, int n, String s) { if(k==a.length){ if(n==0) System.out.println(s); return; } String s2 = s; for(int i=0; i<=a[k]; i++){ _____________________________; //Fill in position s2 += (char)(k+'A'); } } public static void main(String[] args) { int[] a = {4,2,2,1,1,3}; f(a,0,5,""); } }
public class Main { public static void f(int[] a, int k, int n, String s) { if(k==a.length){ if(n==0) System.out.println(s); return; } String s2 = s; for(int i=0; i<=a[k]; i++){ f(a,k+1,n-i,s2); //Fill in position s2 += (char)(k+'A'); } } public static void main(String[] args) { int[] a = {4,2,2,1,1,3}; f(a,0,5,""); /* X The planet is going to send a five person observer mission to planet W. Among them: A The country can send up to four people. B The country can send up to two people. C The country can send up to two people.*/ } }