Algorithm ideas:
Example: Table Tennis Schedule:
Realize:
(1) Decomposition: First, the 8-person schedule is divided into 4 people:
(2) Solution: Then divide the 4-person schedule into 2 people:
(3) Merger: Combined into a 4-person schedule
1 ImportJava.util.Scanner;2 3 Public classFenzhi {4 5 Private StaticScanner in;6 7 Public Static int[] Fenzhi (int[] A,intKintN) {8 9 Ten //The assumption here is that it starts with number 1. One if(n = = 2) { AA[K][1] =K; -A[k][2] = k + 1; -A[k + 1][1] = k + 1; theA[k +) =K; - - returnA; - } + - //divide-and-conquer algorithm, which is essentially two recursion +A = Fenzhi (A, K, N/2); AA = Fenzhi (A, k + N/2, N/2); at - //fill the top right corner - for(inti = k; I < K + N/2; i++) { - for(intj = 1 + n/2; J <= N; J + +) { -A[I][J] = a[i + n/2][j-n/2]; - } in } - to //fill the lower right corner + for(inti = k + n/2; I <= N; i++) { - for(intj = 1 + n/2; J <= N; J + +) { theA[I][J] = a[i-n/2][j-n/2]; * } $ }Panax Notoginseng - returnA; the } + A Public Static voidMain (string[] args) { the +in =NewScanner (system.in); -System.out.print ("Please enter the number you want to start:"); $ intK =in.nextint (); $System.out.print ("Please enter the quantity you want to process:"); - intn =in.nextint (); - the int[] A =New int[n + 1] [n + 1]; -A =Fenzhi (A, k, n);Wuyi the for(inti = 1; I <= N; i++) { - for(intj = 1; J <= N; J + +) { WuSystem.out.print (A[i][j] + "\ T"); - } About System.out.println (); $ } - } - -}View Code
Divide and conquer algorithm