2016年第七届蓝桥杯参赛 Java A 组真题

2016年第七届蓝桥杯参赛 Java A 组真题

第一题

截屏2021-04-05 15.51.20
public class Q1 {

	public static void main(String[] args) {
		int sum = 0, a = 0, d = 1;
		for (int i = 0; i < 100; i++) {
			a += d;
			sum += a;
			d++;
		}
		System.out.println(sum);
	}
}

答案:171700

第二题

截屏2021-04-05 15.55.18
public class Q2 {

	static int MAX_AGE = 200;
	
	public static void main(String[] args) {
		for (int i = 1; i < MAX_AGE; i++) {
			int sum = 0;
			for (int j = i; j < MAX_AGE; j++) {
				sum += j;
				if (sum == 236)
					System.out.println(i);
			}
		}
	}
}

答案:26

第三题

截屏2021-04-05 19.49.33

答案:768

第四题

截屏2021-04-05 16.06.34 截屏2021-04-05 16.06.55

截屏2021-04-05 16.07.46

public class Q4 {

	public static String remain(int[] a) {
		String s = "";
		for (int i = 0; i < a.length; i++) {
			if (a[i] == 0)
				s += (char) (i + 'A');
		}
		return s;
	}
	
	public static void f(String s, int[] a) {
		for (int i = 0; i < a.length; i++) {
			if (a[i] == 1) continue;
			a[i] = 1;
			for (int j = i + 1; j < a.length; j++) {
				if (a[j] == 1) continue;
				a[j] = 1;
				for (int k = j + 1; k < a.length; k++) {
					if (a[k] == 1) continue;
					a[k] = 1;
					System.out.println(s + " " + (char)(i + 'A') + "" + (char)(j + 'A') + "" + (char)(k + 'A') + " " + remain(a));
					a[k] = 0;
				}
				a[j] = 0;
			}
			a[i] = 0;
		}
	}
	
	public static void main(String[] args) {
		int[] a = new int[9];
		a[0] = 1;
		
		for (int b = 1; b < a.length; b++) {
			a[b] = 1;
			for (int c = b + 1; c < a.length; c++) {
				a[c] = 1;
				String s = "A" + (char)(b + 'A') + (char) (c + 'A');
				f(s, a);
				a[c] = 0;
			}
			a[b] = 0;
		}
	}
}

第五题

截屏2021-04-05 16.33.38
public class Q5 {

	public static void f(int[] a, int k, int n, String s) {
		if (k == a.length) {
			if (n == 0)
				System.out.println(s);
			return;
		}
		
		String s2 = s;
		for (int i = 0; i <= a[k]; i++) {
			f(a, k + 1, n - i, s2);
			s2 += (char)(k + 'A');
		}
	}
	
	public static void main(String[] args) {
		int[] a = {4, 2, 2, 1, 1, 3};
		
		f(a, 0, 5, "");
	}
}

第六题

截屏2021-04-05 19.51.34
import java.util.Arrays;

public class Q6 {

	static int[] a = new int[15];
	static boolean[] book = new boolean[15];
	static int ans = 0;
	
	public static void main(String[] args) {
		dfs(1);
		System.out.println(ans);
	}
		
	static void dfs(int k) {
		if (k > 3 && a[1] + a[2] != a[3]) return;
		if (k > 6 && a[4] - a[5] != a[6]) return;
		if (k > 9 && a[7] * a[8] != a[9]) return;	
		if (k == 13) {
			if (a[10] % a[11] == 0 && a[10] / a[11] == a[12])
				ans++;
			return;
		}
		
		for (int i = 1; i <= 13; i++) {
			if (!book[i]) {
				book[i] = true;
				a[k] = i;
				dfs(k + 1);
				book[i] = false;
			}
		}
	}
}

答案:64

第七题

截屏2021-04-05 19.34.52
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Q7 {

	static int[] a = new int[5];
	static boolean[] book = new boolean[15];
	static int[] delta = {-1, 1, -4, 4};
	static int ans = 0;
	
	public static void main(String[] args) {
		dfs(0, 1);
		System.out.println(ans);
	}
	
	static void judge() {
		
		Queue<Integer> queue = new LinkedList<>();
		boolean[] vis = new boolean[15];
		Arrays.fill(vis, true);
		for (int i = 0; i < 5; i++) 
			vis[a[i]] = false;
		queue.add(a[0]);
		vis[a[0]] = true;
		
		int cnt = 0;
		while (!queue.isEmpty()) {
			int cur = queue.poll();
			cnt++;
			for (int i = 0; i < delta.length; i++) {
				int next = cur + delta[i];	
				if (next >= 1 && next <= 12 && !vis[next]) {
					queue.add(next);
					vis[next] = true;
				}
			}
		}
		
		if (cnt == 5) {
			ans++;
		}
	}
	
	static void dfs(int k, int d) {
		if (k == 5) {
			judge();
			return;
		}
		
		for (int i = d; i <= 12; i++) {
			if (!book[i]) {
				book[i] = true;
				a[k] = i;
				dfs(k + 1, i + 1);
				book[i] = false;
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值