-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermulate.java
More file actions
77 lines (75 loc) · 1.96 KB
/
Copy pathPermulate.java
File metadata and controls
77 lines (75 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.sinllychen.string;
import java.util.Scanner;
/**
* 篮球错排问题,思路,先进行全排列之后,然后进行剪枝,根据同一个篮球不在同一个编号的篮子里,以及相邻两个篮球编号不能相同作为原则
* @author sinllychen
*
*/
public class Permulate {
private static int num=0;
// public static int[] geneRandom(int num)
// {
// Random rand=new Random();
// int[] a=new int[num];
// for(int i=0;i<num;i++)
// a[i]=rand.nextInt(num)+1;//0-10不包括10,所以要产生1-10的伪随机数则需要+1
// return a;
// }
public static void permulate(int[] array,int i,int j)
{
if(i>j)
{
if(verify(array))
{
num++;
System.out.print("第"+num+"种放置方法:");
printPermulate(array);
System.out.println();
}
}
else
{
for(int k=i;k<array.length;k++)
{
swap(array,i,k);
permulate(array,i+1,j);
swap(array,k,i);
}
}
}
public static void printPermulate(int[] array)
{
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
}
public static void swap(int[] array,int i,int j)
{
int a=array[i];
array[i]=array[j];
array[j]=a;
}
public static boolean verify(int[] array)
{
for(int i=0;i<array.length;i++)
{
if(array[i]==i+1)
return false;
else if(i+1<array.length&&Math.abs(array[i]-array[i+1])==1)
return false;
}
return true;
}
public static void main(String[] args)
{
System.out.println("请输入数据规模:");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
// int[] array=geneRandom(num);
int[] array=new int[n];
for(int i=0;i<n;i++)
array[i]=i+1;
permulate(array,0,array.length-1);
if(num==0)
System.out.println("对不起,不存在符合要求的放置方式");
}
}