第六届蓝桥杯java B组国赛题目
1.标题:分机号
X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:
751,520,321 都满足要求,而,
766,918,201 就不符合要求。
现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?
请直接提交该数字,不要填写任何多余的内容。
直接全排然后再截取前三个看是否符合降序排列。
我的答案:120
- public class Main {
- static int a[] = {0,1,2,3,4,5,6,7,8,9};
- static int ans = 0;
- public static void main(String[] args) {
- dfs(0);
- System.out.println(ans);
- }
- static void dfs(int offset) {
- if(offset == 3) {
- if(a[0] > a[1] && a[1] > a[2]) {
- ans++;
- }
- //System.out.println(a[0] + ” ” + a[1] + ” ” + a[2]);
- return;
- }
- for(int i = offset; i <= 9; i++) {
- swap(i, offset);
- dfs(offset + 1);
- swap(i, offset);
- }
- }
- static void swap(int aa, int b) {
- int temp = a[aa];
- a[aa] = a[b];
- a[b] = temp;
- }
- }
public class Main {
static int a[] = {0,1,2,3,4,5,6,7,8,9};
static int ans = 0;
public static void main(String[] args) {
dfs(0);
System.out.println(ans);
}
static void dfs(int offset) {
if(offset == 3) {
if(a[0] > a[1] && a[1] > a[2]) {
ans++;
}
//System.out.println(a[0] + " " + a[1] + " " + a[2]);
return;
}
for(int i = offset; i <= 9; i++) {
swap(i, offset);
dfs(offset + 1);
swap(i, offset);
}
}
static void swap(int aa, int b) {
int temp = a[aa];
a[aa] = a[b];
a[b] = temp;
}
}
2.标题:五星填数
如【图1.png】的五星图案节点填上数字:1~12,除去7和11。
要求每条直线上数字和相等。
如图就是恰当的填法。
请你利用计算机搜索所有可能的填法有多少种。
注意:旋转或镜像后相同的算同一种填法。
请提交表示方案数目的整数,不要填写任何其它内容。
思路: 全排,判断符合条件的,再总数除以10
答案: 12
- public class Main {
- static int a[] = {1,2,3,4,5,6,8,9,10,12};
- static int ans = 0;
- public static void main(String[] args) {
- dfs(0);
- System.out.println(ans/10);
- }
- static void dfs(int offset) {
- if(offset == 10) {
- if(test()) {
- ans++;
- }
- return;
- }
- for(int i = offset; i <= 9; i++) {
- swap(i, offset);
- dfs(offset + 1);
- swap(i, offset);
- }
- }
- static boolean test() {
- boolean check = false;
- int sum = a[0] + a[2] + a[5] + a[8];
- if(a[1] + a[2] + a[3] + a[4] != sum) {
- check = true;
- }
- if(a[0] + a[3] + a[6] + a[9] != sum) {
- check = true;
- }
- if(a[1] + a[5] + a[7] + a[9] != sum) {
- check = true;
- }
- if(a[4] + a[6] + a[7] + a[8] != sum) {
- check = true;
- }
- return !check;
- }
- static void swap(int aa, int b) {
- int temp = a[aa];
- a[aa] = a[b];
- a[b] = temp;
- }
- }
public class Main {
static int a[] = {1,2,3,4,5,6,8,9,10,12};
static int ans = 0;
public static void main(String[] args) {
dfs(0);
System.out.println(ans/10);
}
static void dfs(int offset) {
if(offset == 10) {
if(test()) {
ans++;
}
return;
}
for(int i = offset; i <= 9; i++) {
swap(i, offset);
dfs(offset + 1);
swap(i, offset);
}
}
static boolean test() {
boolean check = false;
int sum = a[0] + a[2] + a[5] + a[8];
if(a[1] + a[2] + a[3] + a[4] != sum) {
check = true;
}
if(a[0] + a[3] + a[6] + a[9] != sum) {
check = true;
}
if(a[1] + a[5] + a[7] + a[9] != sum) {
check = true;
}
if(a[4] + a[6] + a[7] + a[8] != sum) {
check = true;
}
return !check;
}
static void swap(int aa, int b) {
int temp = a[aa];
a[aa] = a[b];
a[b] = temp;
}
}
3.标题:显示二叉树
排序二叉树的特征是:
某个节点的左子树的所有节点值都不大于本节点值。
某个节点的右子树的所有节点值都不小于本节点值。
为了能形象地观察二叉树的建立过程,小明写了一段程序来显示出二叉树的结构来。
class BiTree
{
private int v;
private BiTree l;
private BiTree r;
public BiTree(int v){
this.v = v;
}
public void add(BiTree the){
if(the.v < v){
if(l==null) l = the;
else l.add(the);
}
else{
if(r==null) r = the;
else r.add(the);
}
}
public int getHeight(){
int h = 2;
int hl = l==null? 0 : l.getHeight();
int hr = r==null? 0 : r.getHeight();
return h + Math.max(hl,hr);
}
public int getWidth(){
int w = (“”+v).length();
if(l!=null) w += l.getWidth();
if(r!=null) w += r.getWidth();
return w;
}
public void show(){
char[][] buf = new char[getHeight()][getWidth()];
printInBuf(buf, 0, 0);
showBuf(buf);
}
private void showBuf(char[][] x){
for(int i=0; i<x.length; i++){
for(int j=0; j<x[i].length; j++)
System.out.print(x[i][j]==0? ’ ‘:x[i][j]);
System.out.println();
}
}
private void printInBuf(char[][] buf, int x, int y){
String sv = “” + v;
int p1 = l==null? x : l.getRootPos(x);
int p2 = getRootPos(x);
int p3 = r==null? p2 : r.getRootPos(p2+sv.length());
buf[y][p2] = ‘|’;
for(int i=p1; i<=p3; i++) buf[y+1][i]=’-‘;
for(int i=0; i<sv.length(); i++) ________________________________; //填空位置
if(p1<p2) buf[y+1][p1] = ‘/’;
if(p3>p2) buf[y+1][p3] = ‘\\’;
if(l!=null) l.printInBuf(buf,x,y+2);
if(r!=null) r.printInBuf(buf,p2+sv.length(),y+2);
}
private int getRootPos(int x){
return l==null? x : x + l.getWidth();
}
}
public class Main
{
public static void main(String[] args)
{
BiTree tree = new BiTree(500);
tree.add(new BiTree(200));
tree.add(new BiTree(509));
tree.add(new BiTree(100));
tree.add(new BiTree(250));
tree.add(new BiTree(507));
tree.add(new BiTree(600));
tree.add(new BiTree(650));
tree.add(new BiTree(450));
tree.add(new BiTree(510));
tree.add(new BiTree(440));
tree.add(new BiTree(220));
tree.show();
}
}
对于上边的测试数据,应该显示出:
|
/————–500—\
| |
/–200—\ /–509—\
| | | |
100 /–250—\ 507 /–600\
| | | |
220 /–450 510 650
|
440
(如有对齐问题,请参考【图1.png】)
请分析程序逻辑,填写划线部分缺失的代码。
注意,只填写缺少的部分,不要填写已有的代码或符号,也不要加任何说明文字。
我的答案:buf[y+1][p2+i] = sv.charAt(i);
4.标题:穿越雷区
X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。
某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?
已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。
例如:
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -
坦克车只能水平或垂直方向上移动到相邻的区。
数据格式要求:
输入第一行是一个整数n,表示方阵的大小, 4<=n<100
接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。
A,B都只出现一次。
要求输出一个整数,表示坦克从A区到B区的最少移动步数。
如果没有方案,则输出-1
例如:
用户输入:
5
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -
则程序应该输出:
10
资源约定:
峰值内存消耗(含虚拟机) < 512M
CPU消耗 < 2000ms
思路:典型的深搜加回溯
- import java.util.Scanner;
- public class Main{
- public static int xA,yA,xB,yB; //记录A和B的坐标
- public static char[][] map;
- public static int[][] book;
- public static int n;
- private static int stepMin = Integer.MAX_VALUE;
- private static int[][] next = {{-1,0},{1,0},{0,1},{0,-1}};
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- n = sc.nextInt();
- map = new char[n][n];
- book = new int[n][n];
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n; j++) {
- char temp = sc.next().charAt(0);
- if(temp == ’+’ || temp == ’-‘) {
- map[i][j] = temp;
- }else if(temp == ‘A’) {
- xA = i;
- yA = j;
- map[i][j] = ‘A’;
- }else if(temp == ‘B’) {
- xB = i;
- yB = j;
- map[i][j] = ‘B’;
- }
- }
- }
- book[xA][yA] = 1;
- dfs(xA,yA,’0’, 0);
- System.out.println(stepMin);
- }
- public static void dfs(int x, int y, char ch, int step) {
- if(x == xB && y == yB) {
- if(stepMin > step) {
- stepMin = step;
- }
- return ;
- }
- int tx, ty;
- for(int i = 0; i < 4; i++) {
- tx = x + next[i][0];
- ty = y + next[i][1];
- if(tx < 0 || ty < 0 || tx >= n || ty >= n) {
- continue;
- }
- if(map[tx][ty] != ch && book[tx][ty] == 0 ){
- book[tx][ty] = 1;
- dfs(tx, ty, map[tx][ty], step + 1);
- book[tx][ty] = 0;
- }
- }
- }
- }
import java.util.Scanner;
public class Main{
public static int xA,yA,xB,yB; //记录A和B的坐标
public static char[][] map;
public static int[][] book;
public static int n;
private static int stepMin = Integer.MAX_VALUE;
private static int[][] next = {{-1,0},{1,0},{0,1},{0,-1}};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
map = new char[n][n];
book = new int[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
char temp = sc.next().charAt(0);
if(temp == '+' || temp == '-') {
map[i][j] = temp;
}else if(temp == 'A') {
xA = i;
yA = j;
map[i][j] = 'A';
}else if(temp == 'B') {
xB = i;
yB = j;
map[i][j] = 'B';
}
}
}
book[xA][yA] = 1;
dfs(xA,yA,'0', 0);
System.out.println(stepMin);
}
public static void dfs(int x, int y, char ch, int step) {
if(x == xB && y == yB) {
if(stepMin > step) {
stepMin = step;
}
return ;
}
int tx, ty;
for(int i = 0; i < 4; i++) {
tx = x + next[i][0];
ty = y + next[i][1];
if(tx < 0 || ty < 0 || tx >= n || ty >= n) {
continue;
}
if(map[tx][ty] != ch && book[tx][ty] == 0 ){
book[tx][ty] = 1;
dfs(tx, ty, map[tx][ty], step + 1);
book[tx][ty] = 0;
}
}
}
}
5.标题:表格计算
某次无聊中, atm 发现了一个很老的程序。这个程序的功能类似于 Excel ,它对一个表格进行操作。
不妨设表格有 n 行,每行有 m 个格子。
每个格子的内容可以是一个正整数,也可以是一个公式。
公式包括三种:
1. SUM(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的和。
2. AVG(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的平均数。
3. STD(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的标准差。
标准差即为方差的平方根。
方差就是:每个数据与平均值的差的平方的平均值,用来衡量单个数据离开平均数的程度。
公式都不会出现嵌套。
如果这个格子内是一个数,则这个格子的值等于这个数,否则这个格子的值等于格子公式求值结果。
输入这个表格后,程序会输出每个格子的值。atm 觉得这个程序很好玩,他也想实现一下这个程序。
「输入格式」
第一行两个数 n, m 。
接下来 n 行输入一个表格。每行 m 个由空格隔开的字符串,分别表示对应格子的内容。
输入保证不会出现循环依赖的情况,即不会出现两个格子 a 和 b 使得 a 的值依赖 b 的值且 b 的值依赖 a 的值。
「输出格式」
输出一个表格,共 n 行,每行 m 个保留两位小数的实数。
数据保证不会有格子的值超过 1e6 。
「样例输入」
3 2
1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)
「样例输出」
1.00 5.00
2.00 3.00
3.00 1.48
「数据范围」
对于 30% 的数据,满足: n, m <= 5
对于 100% 的数据,满足: n, m <= 50
资源约定:
峰值内存消耗(含虚拟机) < 512M
CPU消耗 < 2000ms
- import java.util.Map;
- import java.util.Scanner;
- //数据不大,每次循环遍历数组处理数据,直到数据的数据都为数字为止。
- public class Main{
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- int m = scanner.nextInt();
- scanner.nextLine();
- String [][]s = new String[n][m];
- for(int i = 0; i < n; i++) {
- String string = scanner.nextLine();
- s[i] = string.split(“ ”);
- }
- boolean add;
- do{
- add = false;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- if(s[i][j].indexOf(“SUM”) != -1){
- String str[] = s[i][j].split(“ |,|:|,| | , | : | , | ”);
- int x1 = Integer.valueOf(str[1]);
- int y1 = Integer.valueOf(str[2]);
- int x2 = Integer.valueOf(str[3]);
- int y2 = Integer.valueOf(str[4]);
- //System.out.println(x1 +” ” + y1 + ” ” + x2 + ” ” + y2);
- float sum = 0;
- for(; x1 <= x2; x1++) {
- for(int y = y1;y <= y2; y++) {
- if(s[x1-1][y-1].indexOf(“SUM”) != -1||
- s[x1-1][y-1].indexOf(“AVG”) != -1||
- s[x1-1][y-1].indexOf(“STD”) != -1){
- add = true;
- x1 = x2+1;
- break;
- }else {
- sum += Float.valueOf(s[x1-1][y-1]);
- }
- }
- }
- if(x1 != x2 + 2){
- s[i][j] = sum + ”“;
- }
- }else if(s[i][j].indexOf(“AVG”) != -1) {
- String str[] = s[i][j].split(“ |,|:|,| | , | : | , | ”);
- int x1 = Integer.valueOf(str[1]);
- int y1 = Integer.valueOf(str[2]);
- int x2 = Integer.valueOf(str[3]);
- int y2 = Integer.valueOf(str[4]);
- float sum = 0;
- int num = (x2-x1+1) * (y2-y1+1);
- for(; x1 <= x2; x1++) {
- for(int y = y1;y <= y2; y++) {
- if(s[x1-1][y-1].indexOf(“SUM”) != -1||
- s[x1-1][y-1].indexOf(“AVG”) != -1||
- s[x1-1][y-1].indexOf(“STD”) != -1){
- add = true;
- x1 = x2+1;
- break;
- }else {
- sum += Float.valueOf(s[x1-1][y-1]);
- }
- }
- }
- if(x1 != x2 + 2){
- s[i][j] = sum/num + ”“;
- }
- }else if(s[i][j].indexOf(“STD”) != -1) {
- String str[] = s[i][j].split(“ |,|:|,| | , | : | , | ”);
- int x1 = Integer.valueOf(str[1]);
- int y1 = Integer.valueOf(str[2]);
- int x2 = Integer.valueOf(str[3]);
- int y2 = Integer.valueOf(str[4]);
- float sum = 0;
- int num = (x2-x1+1) * (y2-y1+1);
- for(; x1 <= x2; x1++) {
- for(int y= y1;y <= y2; y++) {
- if(s[x1-1][y-1].indexOf(“SUM”) != -1||
- s[x1-1][y-1].indexOf(“AVG”) != -1||
- s[x1-1][y-1].indexOf(“STD”) != -1){
- add = true;
- x1 = x2+1;
- break;
- }else {
- sum += Float.valueOf(s[x1-1][y-1]);
- }
- }
- }
- float avg = sum/num;
- if(x1 != x2 + 2){
- sum = 0;
- x1 = Integer.valueOf(str[1]);
- for(;x1 <= x2; x1++){
- for(int y = y1;y <= y2; y++){
- Float tempFloat = (Float.valueOf(s[x1-1][y-1]) - avg)* (Float.valueOf(s[x1-1][y-1]) - avg);
- sum += tempFloat;
- }
- }
- avg = sum/num;
- avg = (float)Math.sqrt(avg);
- s[i][j] = avg + ”“;
- }
- }
- }
- }
- }while(add);
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- System.out.print(String.format(“%.2f”, Float.valueOf(s[i][j])));
- if(j != m-1) {
- System.out.print(“ ”);
- }else {
- System.out.println();
- }
- }
- }
- }
- }
import java.util.Map;
import java.util.Scanner;
//数据不大,每次循环遍历数组处理数据,直到数据的数据都为数字为止。
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.nextLine();
String [][]s = new String[n][m];
for(int i = 0; i < n; i++) {
String string = scanner.nextLine();
s[i] = string.split(" ");
}
boolean add;
do{
add = false;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(s[i][j].indexOf("SUM") != -1){
String str[] = s[i][j].split("\\(|,|:|,|\\)");
int x1 = Integer.valueOf(str[1]);
int y1 = Integer.valueOf(str[2]);
int x2 = Integer.valueOf(str[3]);
int y2 = Integer.valueOf(str[4]);
//System.out.println(x1 +" " + y1 + " " + x2 + " " + y2);
float sum = 0;
for(; x1 <= x2; x1++) {
for(int y = y1;y <= y2; y++) {
if(s[x1-1][y-1].indexOf("SUM") != -1||
s[x1-1][y-1].indexOf("AVG") != -1||
s[x1-1][y-1].indexOf("STD") != -1){
add = true;
x1 = x2+1;
break;
}else {
sum += Float.valueOf(s[x1-1][y-1]);
}
}
}
if(x1 != x2 + 2){
s[i][j] = sum + "";
}
}else if(s[i][j].indexOf("AVG") != -1) {
String str[] = s[i][j].split("\\(|,|:|,|\\)");
int x1 = Integer.valueOf(str[1]);
int y1 = Integer.valueOf(str[2]);
int x2 = Integer.valueOf(str[3]);
int y2 = Integer.valueOf(str[4]);
float sum = 0;
int num = (x2-x1+1) * (y2-y1+1);
for(; x1 <= x2; x1++) {
for(int y = y1;y <= y2; y++) {
if(s[x1-1][y-1].indexOf("SUM") != -1||
s[x1-1][y-1].indexOf("AVG") != -1||
s[x1-1][y-1].indexOf("STD") != -1){
add = true;
x1 = x2+1;
break;
}else {
sum += Float.valueOf(s[x1-1][y-1]);
}
}
}
if(x1 != x2 + 2){
s[i][j] = sum/num + "";
}
}else if(s[i][j].indexOf("STD") != -1) {
String str[] = s[i][j].split("\\(|,|:|,|\\)");
int x1 = Integer.valueOf(str[1]);
int y1 = Integer.valueOf(str[2]);
int x2 = Integer.valueOf(str[3]);
int y2 = Integer.valueOf(str[4]);
float sum = 0;
int num = (x2-x1+1) * (y2-y1+1);
for(; x1 <= x2; x1++) {
for(int y= y1;y <= y2; y++) {
if(s[x1-1][y-1].indexOf("SUM") != -1||
s[x1-1][y-1].indexOf("AVG") != -1||
s[x1-1][y-1].indexOf("STD") != -1){
add = true;
x1 = x2+1;
break;
}else {
sum += Float.valueOf(s[x1-1][y-1]);
}
}
}
float avg = sum/num;
if(x1 != x2 + 2){
sum = 0;
x1 = Integer.valueOf(str[1]);
for(;x1 <= x2; x1++){
for(int y = y1;y <= y2; y++){
Float tempFloat = (Float.valueOf(s[x1-1][y-1]) - avg)* (Float.valueOf(s[x1-1][y-1]) - avg);
sum += tempFloat;
}
}
avg = sum/num;
avg = (float)Math.sqrt(avg);
s[i][j] = avg + "";
}
}
}
}
}while(add);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
System.out.print(String.format("%.2f", Float.valueOf(s[i][j])));
if(j != m-1) {
System.out.print(" ");
}else {
System.out.println();
}
}
}
}
}
6.标题:铺瓷砖
为了让蓝桥杯竞赛更顺利的进行,主办方决定给竞赛的机房重新铺放瓷砖。机房可以看成一个n*m的矩形,而这次使用的瓷砖比较特别,有两种形状,如【图1.png】所示。在铺放瓷砖时,可以旋转。
【输入格式】
输入的第一行包含两个整数,分别表示机房两个方向的长度。
【输出格式】
输出一个整数,表示可行的方案数。这个数可能很大,请输出这个数除以65521的余数。
【样例输入1】
4 4
【样例输出1】
2
【样例说明1】
这两种方案如下【图2.png】所示:
![](https://i-blog.csdnimg.cn/blog_migrate/9cd1dc90a0410492a452aeedcc8c5742.png)
【样例输入2】
2 6
【样例输出2】
4
【数据规模与约定】
对于20%的数据,1<=n, m<=5。
对于50%的数据,1<=n<=100,1<=m<=5。
对于100%的数据,1<=n<=10^15,1<=m<=6。
资源约定:
峰值内存消耗(含虚拟机) < 512M
CPU消耗 < 8000ms
</div>