第八届蓝桥杯Java A组决赛第一题


标题:图书排列

将编号为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);

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值