2016年第七届蓝桥杯省赛 Java B 组真题
第一题

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
第三题

public class Q3 {
static int[] a = new int[10];
static boolean[] book = new boolean[10];
static int ans = 0;
public static void main(String[] args) {
dfs(1);
System.out.println(ans);
}
static void judge() {
int A = a[1], B = a[2], C = a[3];
int DEF = a[4] * 100 + a[5] * 10 + a[6];
int GHI = a[7] * 100 + a[8] * 10 + a[9];
int y = C * GHI;
int x = B * GHI + C * DEF;
if (x % y == 0 && A + x / y == 10) {
ans++;
System.out.println(A + "+" + B + "/" + C + "+" + DEF + "/" + GHI);
}
}
static void dfs(int step) {
if (step == 10) {
judge();
return;
}
for (int i = 1; i < 10; i++) {
if (!book[i]) {
book[i] = true;
a[step] = i;
dfs(step + 1);
book[i] = false;
}
}
}
}
答案:29
第四题


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, "");
}
}
第六题

public class Q6 {
static int[][] a = new int[3][4];
static boolean[] book = new boolean[10];
static int[][] delta = {{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
static int ans = 0;
public static void main(String[] args) {
dfs(1);
System.out.println(ans);
}
static void dfs(int k) {
if (k == 11) {
ans++;
return;
}
int x = k / 4;
int y = k % 4;
for (int i = 0; i < 10; i++) {
if (!book[i]) {
book[i] = true;
a[x][y] = i;
boolean flag = true;
for (int j = 0; j < delta.length; j++) {
int xx = x + delta[j][0];
int yy = y + delta[j][1];
if (xx >= 0 && xx < 3 && yy >= 0 && yy < 4 && !(xx == 0 && yy == 0) && Math.abs(a[xx][yy] - a[x][y]) == 1) {
flag = false;
break;
}
}
if (flag)
dfs(k + 1);
book[i] = false;
}
}
}
}
答案:1580
第七题

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Q7 {
static int[] a = new int[5];
static boolean[] book = new boolean[15];
static int[][] delta = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
static int ans = 0;
static Set<String> set = new HashSet<>();
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++;
int x = (cur - 1) / 4 + 1;
int y = (cur - 1) % 4 + 1;
for (int i = 0; i < delta.length; i++) {
int xx = x + delta[i][0];
int yy = y + delta[i][1];
int next = (xx - 1) * 4 + yy;
if (xx >= 1 && xx <= 3 && yy >= 1 && yy <= 4 && !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;
}
}
}
}
答案:116
第八题

import java.util.Scanner;
public class Q8 {
static int n, a, b, c, d;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (a = 0; a * a <= n / 4; a++) {
for (b = 0; b * b <= n / 3; b++) {
for (c = 0; c * c <= n / 2; c++) {
int d = (int) Math.sqrt(n - a * a - b * b - c * c);
if (d * d == n - a * a - b * b - c * c) {
System.out.println(a + " " + b + " " + c + " " + d);
return;
}
}
}
}
}
}