1. Full arrangement
The idea of full arrangement, in fact, full arrangement is a kind of arrangement and combination, such as 1 2 3, so how many possible combinations are there? Answer: A(3, 3)=3×2×1=6
-
According to our subjective consciousness, there are naturally six kinds of ①1 2 3 ②1 3 2 ③2 1 3 ④2 3 1 ⑤3 1 2 ⑥3 2 1
-
Next, explain the full arrangement idea in detail
-
package test; import java.util.*; public class Main { /* * 全排列的思想,其实全排列就是一种排列组合,比如 1 2 3,那么会有多少种组合的可能呢? * 以我们主观的意识,自然是有 ①1 2 3 ②1 3 2 ③2 1 3 ④2 3 1 ⑤3 1 2 ⑥3 2 1这六种 * 接下来详解全排列思想 * */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); //输入n int[] a=new int[n]; //创建数组a for(int i=0;i<n;i++) { a[i]=sc.nextInt(); // 获取数组a的元素 } perm(a,0,n-1); } public static void perm(int[] a,int l,int r) { //l为左指针,r为右指针 if(l==r) System.out.println(Arrays.toString(a)); else { for(int i=l;i<=r;i++) { swap(a,i,l); // 数组a[i]与a[l]作交换 perm(a,l+1,r); // 余下数组作全排列,固 左指针加1,即 l+1 swap(a,i,l); // 将数组还原防止重复 } } } // 简单的作交换处理 ,定义 交换存储器 temp public static void swap(int[] a,int i,int j) { int temp = a[i]; a[i]=a[j]; a[j]=temp; } }
2. Combination
What is a combination, there is such a problem in mathematics, that is to take n balls out of m balls, how many combinations will there be, such as 1 2 3 4 four balls, take three out, the answer must be C ( 4, 3) = 4. The result is naturally ① 1 2 3 ② 1 2 4 ③ 2 3 4 ④ 1 3 4
Next, the combination idea will be explained in detail
package test;
import java.util.*;
import java.*;
import java.math.*;
public class Main {
static Stack<Integer> stack = new Stack<Integer>();
static int cnt = 0;
public static void main(String[] args) {
//
// 从m个球里(编号为1,2,3...,m)一次取n个球,其中m>n,记录取出球的编号,枚举所有的可能性
int[] data = {1, 2, 3, 4};
recursion3(data, 0, 3, 0);
System.out.println(cnt);
}
public static void recursion3(int[] array, int curnum, int maxnum, int indexnum) {
if (curnum == maxnum) {
cnt++;
System.out.println(stack);
return;
}
for (int i = indexnum; i < array.length; i++) {
if (!stack.contains(array[i])) {
stack.push(array[i]);
recursion3(array, curnum + 1, maxnum, i);
stack.pop();
}
}
}
}