蓝桥杯2016年JavaB组

煤球数目

答案:171700

package Contest2016;

public class 煤球数目 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = new int[101];
		arr[1] = 1;
		int d = 2;
		int sum = arr[1];
		for (int i = 2; i <= 100; i++) {
			arr[i] = arr[i - 1] + d;
			sum += arr[i];
			d++;
		}
		System.out.println(sum);
	}

}

生日蜡烛

答案:26

package Contest2016;

public class 生日蜡烛 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int x = 1; x <= 100; x++) {
			for (int n = 1; n <= 100; n++) {
				int sum = 0;
				for (int i = 0; i <= n; i++) {
					sum += x + i;
				}
				if (sum == 236)
					System.out.println(x + " " + n);
			}
		}
	}

}

凑算式

答案:29

package Contest2016;

public class 凑算式 {
	static int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	static int[] vis;
	static int[] comb;
	static int ans = 0;

	public static void main(String[] args) {
		comb = new int[9];
		vis = new int[9];
		dfs(0);
		System.out.println(ans);
	}

	private static void dfs(int depth) {
		if (depth == 9) {
			if (check()) {
				ans++;
			}
			return;
		}

		for (int i = 0; i < arr.length; i++) {
			if (vis[i] == 0) {
				vis[i] = 1;
				comb[depth] = arr[i];
				dfs(depth + 1);
				vis[i] = 0;
			}
		}
	}

	private static boolean check() {
		double first = comb[0];
		double second = comb[1] * 1.0 / comb[2];
		double third = (comb[3] * 100 + comb[4] * 10 + comb[5]) * 1.0 / (comb[6] * 100 + comb[7] * 10 + comb[8]);
		return first + second + third == 10;
	}

}

分小组

package Contest2016;

public class 分小组 {
	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) ('A' + i) + (char) ('A' + j) + (char) ('A' + k) + " " + 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;
		}
	}
}

抽签

package Contest2016;

public class 抽签 {
	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, "");
	}
}

方格填数

package Contest2016;

public class 方格填数 {
	static int[][] maze;
	static int[] vis;
	static int[] dx = { 1, -1, 1, 0, -1, 1, 0, -1 };
	static int[] dy = { 0, 0, 1, 1, 1, -1, -1, -1 };
	static int ans = 0;

	public static void main(String[] args) {
		maze = new int[3][4];
		vis = new int[10];
		dfs(0, 1);
		System.out.println(ans);
	}

	public static void dfs(int x, int y) {
		if (x == 2 && y == 3) {
			if (check()) {
				ans++;
			}
			return;
		}

		for (int i = 0; i <= 9; i++) {
			if (vis[i] == 0) {
				vis[i] = 1;
				maze[x][y] = i;
				int newX = x;
				int newY = y + 1;
				if (newY >= 4) {
					newX = x + 1;
					newY = 0;
				}
				dfs(newX, newY);
				vis[i] = 0;
			}
		}
	}

	private static boolean outRange(int newX, int newY) {
		if (!(0 <= newX && newX <= 2))
			return true;
		if (!(0 <= newY && newY <= 3))
			return true;
		if (newX == 0 && newY == 0)
			return true;
		if (newX == 2 && newY == 3)
			return true;
		return false;
	}

	private static boolean check() {
		int x = 0;
		int y = 1;
		while (true) {
			for (int i = 0; i < dx.length; i++) {
				int newX = x + dx[i];
				int newY = y + dy[i];
				if (!outRange(newX, newY)) {
					int temp = maze[newX][newY] - maze[x][y];
					if (temp == 1 || temp == -1)
						return false;
				}
			}
			if (y + 1 >= 4) {
				x++;
				y = 0;
			} else
				y++;
			if (x == 2 && y == 3)
				break;
		}
		return true;
	}
}

待更新

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值