算法笔记_122:蓝桥杯第七届省赛（Java语言A组）试题解答

目录

1 煤球数目

2 生日蜡烛

3 搭积木

4 分小组

5 抽签

6 寒假作业

7 剪邮票

8 取球博弈

9 交换瓶子

10 压缩变换

前言：以下试题解答代码部分仅供参考，若有不当之处，还请路过的同学提醒一下~

1 煤球数目

煤球数目

....

171700

public class Main {

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

2 生日蜡烛

生日蜡烛

26

public class Main {

public static void main(String[] args) {
for(int i = 1;i <= 100;i++) {
int sum = i;
for(int j = i + 1;j <= 100;j++) {
sum += j;
if(sum == 236)
System.out.println("i = "+i+", j = "+j);
if(sum > 236)
break;
}
}
}
}

3 搭积木

搭积木

0
1 2
3 4 5
6 7 8 9

0
3 1
7 5 2
9 8 6 4

768


public class Main {
public static int count = 0;

public void swap(int[] A, int a, int b) {
int temp = A[a];
A[a] = A[b];
A[b] = temp;
}

public void dfs(int[] A, int step) {
if(step == A.length) {
if(check(A)) {
count++;
}
return;
} else {
for(int i = step;i < A.length;i++) {
swap(A, i, step);
dfs(A, step + 1);
swap(A, i, step);
}
}
return;
}

public boolean check(int[] A) {
if(A[0] < A[1] && A[0] < A[2]) {
if(A[1] < A[3] && A[1] < A[4] && A[2] < A[4] && A[2] < A[5]) {
if(A[3] < A[6] && A[3] < A[7] && A[4] < A[7] && A[4] < A[8] && A[5] < A[8] && A[5] < A[9])
return true;
}
}
return false;
}

public static void main(String[] args) {
Main test = new Main();
int[] A = {0,1,2,3,4,5,6,7,8,9};
test.dfs(A, 0);
System.out.println(count);
}

}

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;
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;
}
}
}

s +" "+(char)(i+'A') + (char)(j+'A') + (char)(k+'A')+" "+remain(a)

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++){
_____________________________;   //填空位置
s2 += (char)(k+'A');
}
}

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

f(a,0,5,"");
}
}

f(a, k + 1, n - i,s2)

6 寒假作业

寒假作业

□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □

(如果显示不出来，可以参见【图1.jpg】)

6  + 7 = 13
9  - 8 = 1
3  * 4 = 12
10 / 2 = 5

7  + 6 = 13
9  - 8 = 1
3  * 4 = 12
10 / 2 = 5

64

//方法1：DFS搜索
import java.util.ArrayList;

public class Main {
public ArrayList<Integer> list = new ArrayList<Integer>();
public static int count = 0;

public boolean check() {
int len = list.size();
if(len >= 3) {
if(list.get(0) + list.get(1) != list.get(2))
return false;
}
if(len >= 6) {
if(list.get(3) - list.get(4) != list.get(5))
return false;
}
if(len >= 9) {
if(list.get(6) * list.get(7) != list.get(8))
return false;
}
if(len >= 12) {
if(list.get(9) / list.get(10) != list.get(11) || list.get(9) % list.get(10) != 0)
return false;
}
return true;
}

public void dfs(int step) {
if(step == 12) {
System.out.println(list);
count++;
return;
} else {
for(int i = 1;i <= 13;i++) {
if(list.contains(i))
continue;
if(check() == false) {   //类似八皇后问题，此处进行减枝操作
list.remove(list.indexOf(i));
continue;
}
dfs(step + 1);
list.remove(list.size() - 1);
}
}
return;
}

public static void main(String[] args) {
Main test = new Main();
test.dfs(0);
System.out.println(count);
}
}

//方法2：蛮力枚举
import java.util.ArrayList;
import java.util.Collections;

public class Main1 {
public static long count = 0;

public void printResult() {
for(int a1 = 1;a1 <= 13;a1++) {
for(int a2 = 1;a2 <= 13;a2++) {
if(a1 == a2 || a1 + a2 > 13)
continue;
for(int a3 = 1;a3 <= 13;a3++) {
for(int a4 = 1;a4 <= 13;a4++) {
if(a3 == a4 || a3 - a4 < 1)
continue;
for(int a5 = 1;a5 <= 13;a5++) {
for(int a6 = 1;a6 <= 13;a6++) {
if(a5 == a6 || a5 * a6 > 13)
continue;
for(int a7 = 1;a7 <= 13;a7++) {
for(int a8 = 1;a8 <= 13;a8++) {
if(a7 == a8 || a7 % a8 != 0)
continue;
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> tempList = new ArrayList<Integer>();
for(int i = 0;i < list.size();i++)
Collections.sort(list);
boolean judge = true;
for(int i = 1;i < list.size();i++) {
if(list.get(i - 1) == list.get(i)) {
judge = false;
break;
}else {
continue;
}
}

if(judge == true) {
System.out.println(tempList);
count++;
}
}
}
}
}
}
}
}
}
}

public static void main(String[] args) {
Main1 test = new Main1();
test.printResult();
System.out.println(count);
}
}

7 剪邮票

剪邮票

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

116

import java.util.ArrayList;

public class Main {
public ArrayList<Integer> list = new ArrayList<Integer>();
public static int[] A = {1,2,3,4,5,6,7,8,9,10,11,12};
public static boolean[] visited = new boolean[5];
public static long count = 0;

public void dfs(int step) {
while(step < A.length) {
if(list.size() == 5) {
if(check()) {
System.out.println(list);
count++;
}

}
step++;
dfs(step);
list.remove(list.size() - 1);
}
}

public boolean check() {
for(int i = 0;i < 5;i++)
visited[i] = false;
int start = list.get(0);
dfsPath(start, 0);
for(int i = 0;i < 5;i++) {
if(visited[i] == false)
return false;
}
return true;
}

public void dfsPath(int a, int i) {
visited[i] = true;
int start1 = a + 1;
int start2 = a - 1;
int start3 = a + 4;
int start4 = a - 4;
int r = (a - 1) / 4;
if(list.contains(start1) && (start1 - 1) / 4 == r && !visited[list.indexOf(start1)])
dfsPath(start1, list.indexOf(start1));
if(list.contains(start2) && (start2 - 1) / 4 == r && !visited[list.indexOf(start2)])
dfsPath(start2, list.indexOf(start2));
if(list.contains(start3) && !visited[list.indexOf(start3)])
dfsPath(start3, list.indexOf(start3));
if(list.contains(start4) && !visited[list.indexOf(start4)])
dfsPath(start4, list.indexOf(start4));
}

public static void main(String[] args) {
Main test = new Main();
test.dfs(0);
System.out.println(count);
}
}

8 取球博弈

取球博弈

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

import java.util.Scanner;

public class Main {
public static int[] value = new int[1000];
public static int[] getN = new int[3];
public static int[] init = new int[5];
public static char[] result = {'-','0','0','+'};

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for(int i = 0;i < 3;i++)
getN[i] = in.nextInt();
for(int i = 0;i < 5;i++)
init[i] = in.nextInt();
int minN = Math.min(getN[0], Math.min(getN[1], getN[2]));
if(minN % 2 == 1) //此处关于平局，两者是均为奇数，还是均为偶数问题，这里处理原因，是我自己猜想
value[0] = 1;  //代表平局,两者默认均为奇数个
else
value[0] = 2;  //代表平局,两者默认均为偶数个
for(int i = 1;i < minN;i++)
value[i] = 2;  //0代表负,1和2代表平局，3代表胜
for(int i = minN;i < 1000;i++) {
int temp = 0;      //初始化，当前i个球，先取者必输

for(int j = 0;j < 3;j++) {
if(i - getN[j] < 0)
continue;
if(i - getN[j] == 0 && getN[j] % 2 == 1)
temp = 3;
if(value[i - getN[j]] == 0) {  //表示i - getN[j]个球先取时，必输
if(getN[j] % 2 == 0)
temp = 3;
//此时最终结果为两人取的球均为偶数个,但是若temp取三个数中另外一个数时
//，是必赢结果，则舍弃这个平局结果
else
temp = 2 > temp ? 2 : temp;
}
if(value[i - getN[j]] == 1) { //表示i - getN[j]个球先取时，两人取球均为奇数个
if(getN[j] % 2 == 0)
temp = 1 > temp ? 1 : temp;  //此处做比较同上
}
if(value[i - getN[j]] == 2) {//表示i - getN[j]个球先取时,两人取球均为偶数个
if(getN[j] % 2 == 1)
temp = 3;     //此种情况出现，必赢，不必做比较判断
else
temp = 2 > temp ? 2 : temp;   //此处比较同上，排除必输情况
}
if(value[i - getN[j]] == 3) {//表示i - getN[j]个球先取时,必赢
if(getN[j] % 2 == 1)
temp = 1 > temp ? 1 : temp;
}
}

value[i] = temp;  //当前i个球，先取者最终输赢结果
}
//打印题意最终结果
for(int i = 0;i < 5;i++)
System.out.print(result[value[init[i]]]+" ");
}
}

9 交换瓶子

交换瓶子

2 1 3 5 4

1 2 3 4 5

5
3 1 2 5 4

3

5
5 4 3 2 1

2

CPU消耗  < 1000ms

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
public static int count = 0;
public static ArrayList<Integer> list = new ArrayList<Integer>();

public void getResult() {
ArrayList<Integer> tempList = new ArrayList<Integer>();
int len = list.size();
for(int i = 0;i < len;i++)
Collections.sort(tempList);    //对链表中元素进行从小到大排序
while(true) {
for(int i = 0;i < len;i++) {
int value = list.get(i);
int tempi = tempList.indexOf(value);
if(i == tempi)
continue;
else {
Collections.swap(list, i, tempi);
count++;
}
}
boolean judge = true;
for(int i = 0;i < len;i++) {
if(list.get(i) != tempList.get(i)) {
judge = false;
break;
}
}
if(judge == true)
break;
}
return;
}

public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 1;i <= n;i++) {
int a = in.nextInt();
}
test.getResult();
System.out.println(count);
}
}

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
public static int[] arrayN;

public int getValue(int[] tempN, int position) {
if(position == 0)
return -1;
int i = position - 1;
for(;i >= 0;i--) {
if(tempN[i] == tempN[position])
break;
if(i == 0)
i--;
}
if(i < 0)
return -1;
ArrayList<Integer> list = new ArrayList<Integer>();
i = i + 1;
for(;i < position;i++)
if(list.size() == 0)
return 0;
Collections.sort(list);
int count = 1;
for(int j = 1;j < list.size();j++) {
if(list.get(j) != list.get(j - 1))
count++;
}
return count;
}

public void getResult() {
int len = arrayN.length;
int[] tempN = new int[len];
for(int i = 0;i < len;i++)
tempN[i] = arrayN[i];
for(int i = 0;i < len;i++) {
int value = getValue(tempN, i);
if(value == -1)
arrayN[i] = (-1) * arrayN[i];
else
arrayN[i] = value;
}
//打印最终结果
for(int i = 0;i < len;i++)
System.out.print(arrayN[i]+" ");
}

public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
arrayN = new int[n];
for(int i = 0;i < n;i++)
arrayN[i] = in.nextInt();
test.getResult();
}

}

posted @ 2017-04-01 14:09  舞动的心  阅读(2235)  评论(0编辑  收藏