iceq

2016年第七届蓝桥杯java B组省赛试题

• 1-3、结果填空
• 4-5、代码填空
• 6-7、结果填空
• 8-10、程序设计

1.煤球数目  (结果填空)

....

``````public class Main1 {
public static void main(String[] args) {
int a[] = new int[101];
for (int i = 1; i <= 100; i++) {
a[i] = i * (i + 1) / 2;
}
int sum = 0;

for (int i = 1; i <= 100; i++) {
sum = sum + a[i];
}
System.out.println(sum);
}
}``````

``171700``

2、生日蜡烛  (结果填空)

``26``

``````public class Main1 {
public static void main(String[] args) {

//i代表举办了i个生日party
for (int startAge = 1; startAge < 1000; startAge++) {
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += startAge + i;
if (sum == 236) {
System.out.println("startAge = " + startAge);
}
}
}
}
}``````

``````public class Main {
public static void main(String[] args) {
for (int i = 1; i < 20; i++) {
int isDivisible = (472+i-i*i) % (2*i);
if (isDivisible==0){
System.out.printf("年龄："+(472+i-i*i) / (2*i));
System.out.printf(",过了%d个生日\n", i);
}
}
}
}
``````

3、凑算式  (结果填空)

6+8/3+952/714 就是一种解法，
5+3/1+972/486 是另一种解法。

``````import java.util.Stack;
/**
* Created by harry on 2016/10/16.
*/

public class Main {
public static int count = 0;
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
for (int a = 1; a < 10; a++) {
stack.push(a);
fun(stack);
stack.pop();
}
System.out.println(count);
}

private static void fun(Stack<Integer> stack) {
for (int j = 1; j < 10; j++) {
if (stack.size() == 9) {
int a = stack.get(0);
int b = stack.get(1);
int c = stack.get(2);
int d = stack.get(3);
int e = stack.get(4);
int f = stack.get(5);
int g = stack.get(6);
int h = stack.get(7);
int i = stack.get(8);
int DEF = d * 100 + e * 10 + f;
int GHI = g * 100 + h * 10 + i;
int left = a * c * GHI + b * GHI + DEF * c;
int right = 10 * c * GHI;
if (left == right ) {
count++;
}
return;
}
if (!stack.contains(j)) {
stack.push(j);
fun(stack);
stack.pop();
}
}
}
}``````

``29``

4、 分小组  (代码填空)

9名运动员参加比赛，需要分3组进行预赛。

ABC DEF GHI

ABC DEG FHI

ABC DEH FGI

ABC DEI FGH

ABC DFG EHI

ABC DFH EGI

ABC DFI EGH

ABC DGH EFI

ABC DGI EFH

ABC DHI EFG

ABC EFG DHI

ABC EFH DGI

ABC EFI DGH

ABC EGH DFI

ABC EGI DFH

ABC EHI DFG

ABC FGH DEI

ABC FGI DEH

ABC FHI DEG

ABC GHI DEF

ABD CEF GHI

ABD CEG FHI

ABD CEH FGI

ABD CEI FGH

ABD CFG EHI

ABD CFH EGI

ABD CFI EGH

ABD CGH EFI

ABD CGI EFH

ABD CHI EFG

ABD EFG CHI

..... (以下省略，总共560行)。

``````public class A
{
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;
//此处应填 s+" "+(char)(i+'A')+(char)(j+'A')+(char)(k+'A')+" "+remain(a)
System.out.println(__________________________________);  //填空位置
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;
}
}
}  ``````

5、抽签  (代码填空)

X星球要派出一个5人组成的观察团前往W星。

A国最多可以派出4人。

B国最多可以派出2人。

C国最多可以派出2人。

....

DEFFF

CEFFF

CDFFF

CDEFF

CCFFF

CCEFF

CCDFF

CCDEF

BEFFF

BDFFF

BDEFF

BCFFF

BCEFF

BCDFF

BCDEF

....

(以下省略，总共101行)

``````public class A {
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,5-s2.length(),s2);
s2 += (char) (k + 'A');
}
}

public static void main(String[] args) {
int[] a = {4, 2, 2, 1, 1, 3};
f(a, 0, 5, "");
}
}``````

6、方格填数（结果填空）

+--+--+--+

（如果显示有问题，也可以参看【图1.jpg】）

（左右、上下、对角都算相邻）

``````package seven.six;

import java.util.Stack;
/**
* A[2]reated on 2016/10/26 17:57
*
* @author harry
* @version 1.0
*/
public class Test {
public static int count = 0;
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
Stack<Integer> stack = new Stack<>();
fun(stack);
long endTime = System.currentTimeMillis();
System.out.println("时间: " + (endTime - startTime));
System.out.println("count=" + count);
}

private static void fun(Stack<Integer> stack) {
if (stack.size() == 10) {
int A[] = new int[10];
A[0] = stack.get(0);
A[1] = stack.get(1);
A[2] = stack.get(2);
A[3] = stack.get(3);
A[4] = stack.get(4);
A[5] = stack.get(5);
A[6] = stack.get(6);
A[7] = stack.get(7);
A[8] = stack.get(8);
A[9] = stack.get(9);
boolean one = isNeibor(A[0], A[1], A[5], A[4], A[3]) && isNeibor(A[1], A[0], A[2], A[6], A[5], A[4]) && isNeibor(A[2], A[1], A[5], A[6]);
boolean two = isNeibor(A[3], A[0], A[4], A[8], A[7]) && isNeibor(A[4], A[0], A[8], A[5], A[9], A[8], A[7], A[3])
&& isNeibor(A[5], A[0], A[1], A[2], A[6], A[9], A[8], A[4]) && isNeibor(A[6], A[2], A[1], A[5], A[9]);
boolean three = isNeibor(A[7],  A[8]) && isNeibor(A[8], A[9]);

if (one && two && three) {
System.out.printf("%d,%d,%d, %d,%d,%d,%d, %d,%d,%d\n", A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9]);
count++;
}
return;
}
for (int j = 0; j < 10; j++) {
if (!stack.contains(j)) {
stack.push(j);
fun(stack);
stack.pop();
}
}
}

public static  boolean isNeibor(int ...args){
int first = args[0];
for (int i = 1; i < args.length; i++) {
if (Math.abs(first - args[i]) == 1) {
return false;
}
}
return true;
}
}``````

``1580``

7、剪邮票  (结果填空)

（仅仅连接一个角不算相连）

\$\$ 图1 \$\$

\$\$ 图2 \$\$

\$\$ 图3 \$\$

``116``

``````public class Main {
private static int mp[] = {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14};
private static int aa[] = new int[5];
private static int vis[] = new int[5];
private static int sum = 0;
private static int b[] = {-1, 1, -5, +5};

public static void main(String[] args) {
for (int a = 0; a < 12; a++)
for (int b = a + 1; b < 12; b++)
for (int c = b + 1; c < 12; c++)
for (int d = c + 1; d < 12; d++)
for (int e = d + 1; e < 12; e++) {
aa[0] = mp[a];
aa[1] = mp[b];
aa[2] = mp[c];
aa[3] = mp[d];
aa[4] = mp[e];
for (int i = 0; i < 5; i++) {
vis[i] = 0;
}
vis[0] = 1;
dfs(0);
int flag = 1;
for (int i = 0; i < 5; i++) {
if (vis[i] != 1) {
flag = 0;
break;
}
}
if (flag == 0) continue;
else{
sum++;
}
}
System.out.println(sum);
}

public static void dfs(int n) {
for (int i = 0; i < 4; i++) {
int t = aa[n] + b[i];
if (t < 1 || t > 14 || t == 5 || t == 10) continue;

for (int j = 0; j < 5; j++)
if (!(vis[j] == 1) && aa[j] == t) {
vis[j] = 1;
dfs(j);
}
}
}
}``````

8、四平方和  (程序设计)

5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
（^符号表示乘方的意思）

0 <= a <= b <= c <= d

5

0 0 1 2

12

0 2 2 2

773535

1 1 267 838

CPU消耗  < 3000ms

``````public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Number = in.nextInt();
int maxSubNumber = (int) Math.sqrt(Number);
Loop:
for (int a = 0; a <= maxSubNumber; a++) {
for (int b = a; b < maxSubNumber; b++) {
for (int c = b; c <= maxSubNumber; c++) {
for (int d = c; d <= maxSubNumber; d++) {
int S = a * a + b * b + c * c + d * d;
if (S == Number) {
System.out.print(a);
System.out.print(" " + b);
System.out.print(" " + c);
System.out.print(" " + d);
break Loop;
}
}
}
}
}
}
}``````

9、取球博弈  (程序设计)

1 2 3

1 2 3 4 5

+ 0 + 0 -

1 4 5

10 11 12 13 15

0 - 0 + +

2 3 5

7 8 9 10 11

+ 0 0 0 0

CPU消耗  < 3000ms

``````   今盒子里有n个小球，A、B两人轮流从盒中取球，每个人都可以看到另一个人取了多少个，也可以看到盒中还剩下多少个，并且两人都很聪明，不会做出错误的判断。

我们约定：
每个人从盒子中取出的球的数目必须是：1，3，7或者8个。

轮到某一方取球时不能弃权！

A先取球，然后双方交替取球，直到取完。

被迫拿到最后一个球的一方为负方（输方）

请编程确定出在双方都不判断失误的情况下，对于特定的初始球数，A是否能赢？

4
1
2
10
18

0
1
1
0``````

``````import java.util.Scanner;

public class Test1 {//简单博弈，找出必败点必胜点
static int b[] = {1, 3, 7, 8};
static boolean a[] = new boolean[10010];

public static void main(String[] args) {
Init();
Scanner input = new Scanner(System.in);
int N = input.nextInt();
while (N-- > 0) {
int n = input.nextInt();
System.out.println(a[n] ? 1 : 0);
}
}

private static void Init() {
for (int i = 1; i < 10000; i++) {//从1开始
if (!a[i]) {
for (int j = 0; j < 4; j++)
a[i + b[j]] = true;
}
}
}
}``````

10、压缩变换(程序设计)

a1: 1未出现过，所以a1变为-1；
a2: 2未出现过，所以a2变为-2；
a3: 2出现过，最后一次为原序列的a2，在a2后、a3前有0种数字，所以a3变为0；
a4: 1出现过，最后一次为原序列的a1，在a1后、a4前有1种数字，所以a4变为1；
a5: 2出现过，最后一次为原序列的a3，在a3后、a5前有1种数字，所以a5变为1。

5
1 2 2 1 2

-1 -2 0 1 1

12
1 1 2 3 2 3 1 2 2 2 3 1

-1 0 -2 -3 1 1 2 2 0 0 2 2

CPU消耗  < 3000ms

``````public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] a = new int[length];

for (int i = 0; i < length; i++) {
a[i] = scanner.nextInt();
}

Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < length; i++) {
if (!map.keySet().contains(a[i])) {
map.put(a[i], i);
System.out.print(-a[i] + " ");
} else {
Integer position = map.get(a[i]);
map.put(a[i], i);
List<Integer> tmpList = new ArrayList<>();
for (int j = position + 1; j < i; j++) {
if (!tmpList.contains(a[j])) {
tmpList.add(a[j]);
}
}
System.out.print(tmpList.size() + " ");
}
}
}
}``````

211 声望
19 粉丝
0 条评论