2016年第七届蓝桥杯参赛 Java A 组真题
第一题
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
第二题
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
第三题
答案:768
第四题
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;
}
}
}
第五题
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, "");
}
}
第六题
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
第七题
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;
}
}
}
}