标题:图书排列
将编号为1~10的10本书排放在书架上,要求编号相邻的书不能放在相邻的位置。
请计算一共有多少种不同的排列方案。
注意,需要提交的是一个整数,不要填写任何多余的内容。
答案:479306
package name1;
import java.util.*;
public class textt {
static HashSet<String> arrs = new HashSet<String>();
static int sum = 0;
public static void f(int[] s1, int n) {
if (n == s1.length) {
boolean Flaen = false;
for (int i = 0; i < s1.length - 1; i++) {
int s2 = s1[i] - '0';
int s3 = s1[i + 1] - '0' - 1;
int s4 = s1[i] - '0';
int s5 = s1[i + 1] - '0' + 1;
if (s2 == s3 | s4 == s5) {
Flaen = false;
break;
} else {
Flaen = true;
}
}
if (Flaen == true) {
sum++;
}
}
for (int i = n; i < s1.length; i++) {
int temp = s1[i];
s1[i] = s1[n];
s1[n] = temp;
f(s1, n + 1);
temp = s1[i];
s1[i] = s1[n];
s1[n] = temp;
}
}
public static void main(String[] args) {
int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
f(a, 0);
System.out.println(sum);
}
}