When I joined the Blue Bridge Cup, I found that most of the problems could be brute force. So this time I would like to summarize:
General solution to the problem of full permutation, for example: (no repetition of all permutations) (with repeated selection) (the question of selecting m number from n number and then arranging)
Here I try to summarize:
For example general questions like to ask 1-9 how many kinds of full arrangement, then the code is as follows
ImportJava.util.Stack; Public classTest1 {//coping with the problem of full alignment//divided into three cases: a full permutation with no repetition, a full array of duplicates, and a full array of arbitrary numbers.//It's written in a non-repeating full arrangement .//like a 1-9 full arrangement. Private Static intCount=0; Public Static voidMain (string[] args) {Stack<Integer> stack =NewStack<integer>(); for(inti=1;i<=9;i++) {Stack.push (i); Fun (stack); Stack.pop (); } System.out.println (count); } Private Static voidFun (stack<integer>stack) { if(Stack.size () ==9) {Count++;
Return } for(intj=1;j<=9;j++){ if(!Stack.contains (j)) {Stack.push (j); Fun (stack); Stack.pop (); } } }}
It's all about the stack I used to do.
Randomly select 4 data from 1-9 data results as follows
What we need to be aware of is that the front is not necessarily aligned to the back.
Importjava.util.ArrayList;//here I would like to choose 4 numbers from 1-9 to make a combination.//The combination here is only considered the combination of not considering the order Public classtest1b {//set a global variable to store the data that needs to be stored Private StaticArraylist<integer> ArrayList =NewArraylist<integer>(); Private Static intCount=0; Public Static voidMain (string[] args) {//Array to select int[] a={1,2,3,4,5,6,7,8,9}; //Select the number of data intK=4; if(k>a.length| | K<=0){ return; } //The number of subscript data to be selected for storage location Storage arrayFun (a,0, K); System.out.println (count); } Private Static voidFunint[] A,intIndexintk) {if(k==1){ for(inti=index;i<a.length;i++) {Arraylist.add (a[i]); System.out.println (arraylist.tostring ()+""); Count++; Arraylist.remove ((Object) a[i]); } }Else if(k>1){ for(inti=index;i<a.length;i++) {Arraylist.add (a[i]); //The K value needs to be reduced because as the load data inevitably brings the K value down I value increasesFun (A, i+1, k-1); Arraylist.remove ((Object) a[i]); } }Else{ return; } }}
Then choose the full array of four numbers from 1-9.
Importjava.util.ArrayList;//to repeat the arrangement from 1-9 to pick out is four numbers to arrange Public classTEST1D {Private StaticArraylist<integer> ArrayList =NewArraylist<integer>(); Private Static intCount=0; Public Static voidMain (string[] args) {int[] a={1,2,3,4,5,6,7,8,9}; intK=4; Fun (K,a); System.out.println (count); } Private Static voidFunintKint[] A) {if(k==1){ for(inti=0;i<a.length;i++) {Arraylist.add (a[i]); System.out.println (Arraylist.tostring ()); Arraylist.remove ((Object) a[i]); Count++; } }Else if(k>1){ for(inti=0;i<a.length;i++) {Arraylist.add (a[i]); Fun (k-1, Removelement (A)); Arraylist.remove ((Object) a[i]); } }Else{ return; } } //The purpose of this function is to compare the overlap between the array and the ArrayList if overlapping will remove the data from the array. Private Static int[] Removelement (int[] A) {ArrayList<Integer> list =NewArraylist<integer>(); for(inti=0;i<a.length;i++){ //iterating through an array//indicates that there is no Booleanexit=true; for(intJ=0;j<arraylist.size (); j + +){ //Traverse ArrayList if(a[i]==Arraylist.get (j)) {Exit=false; Break; } } if(exit) {List.add (a[i]); } } int[] b=New int[List.size ()]; for(intM=0;m<list.size (); m++) {B[m]=List.get (m); } returnB; }}
Then it's not going to be heavy. Select 4 numbers from 1-9 each number can be repeated four times the arrangement
Importjava.util.ArrayList; Public classtest1e {Private StaticArraylist<integer> ArrayList =NewArraylist<integer>(); Private Static intCount=0; Public Static voidMain (string[] args) {int[] A ={1,2,3,4,5,6,7,8,9}; intK=4; Fun (0, K,a); System.out.println (count); } Private Static voidFunintIndexintKint[] A) {if(k==1){ for(inti=0;i<a.length;i++) {Arraylist.add (a[i]); System.out.println (Arraylist.tostring ()); Arraylist.remove (Arraylist.size ()-1); Count++; } }Else if(k>1){ for(inti=0;i<a.length;i++) {Arraylist.add (a[i]); Fun (i, K-1, A); Arraylist.remove (Arraylist.size ()-1); } }Else{ return; } }}
Okay, the general permutation problem solves the hope of helping you.
Common algorithmic problems are all arranged