蓝桥杯-决赛B组第六届java

目录

第1题 分机号

第2题 五星填数

第3题 显示二叉树

第4题 穿越雷区

第5题 表格计算

第6题 铺瓷砖


第1题 分机号

标题:分机号

X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:

751,520,321 都满足要求,而,
766,918,201 就不符合要求。

现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?

请直接提交该数字,不要填写任何多余的内容。


答案:120

思路:全排列,放到一个长度为三的数组里,然后比较一下是不是降序1,然后计数

public class Main {

	public static int [] a = new int [3];
	public static int count = 0;
	public static boolean[] b = new boolean [10];
	public static void main(String[] args) {
		dfs(0);
		System.out.println(count);
	}
	public static void dfs(int te){
		if(te==3){
			if(show()){
				count++;
			}
			return;
		}
		for(int i=0;i<=9;i++){
			if(b[i]){
				continue;
			}
			b[i] = true;
			a[te] = i;
			dfs(te+1);
			b[i] = false;
		}
	}
	public static boolean show(){
		if(a[0]>a[1]&&a[1]>a[2]){
			return true;
		}
		return false;
	}
}
//120

第2题 五星填数

标题:五星填数

如【图1.png】的五星图案节点填上数字:1~12,除去7和11。
要求每条直线上数字和相等。

如图就是恰当的填法。

请你利用计算机搜索所有可能的填法有多少种。
注意:旋转或镜像后相同的算同一种填法。

请提交表示方案数目的整数,不要填写任何其它内容。


答案:12

思路:全排列

public class Main {

	public static int [] a = new int [10];
	public static int count=0;
	public static boolean [] b = new boolean [13];
	public static void main(String[] args) {
		dfs(0);
		System.out.println(count/10);
	}
	public static void dfs(int te){
		if(te==10){
			if(show()){
				count++;
			}
			return;
		}
		for(int i=1;i<=12;i++){
			if(i==7||i==11){
				continue;
			}
			if(b[i]){
				continue;
			}
			b[i] = true;
			a[te] = i;
			dfs(te+1);
			b[i] = false;
			
		}
	}
	public static boolean show(){
		int a1 = a[0]+a[1]+a[3]+a[4];
		int a2 = a[2]+a[3]+a[5]+a[6];
		int a3 = a[4]+a[5]+a[7]+a[8];
		int a4 = a[6]+a[7]+a[9]+a[0];
		int a5 = a[8]+a[9]+a[1]+a[2];
		if(a1==a2&&a1==a3&&a1==a4&&a1==a5&&a2==a3&&a2==a4&&a2==a5&&a3==a4&&a3==a5&&a4==a5){
			return true;
		}
		return false;
	}
}
//12

第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】)

请分析程序逻辑,填写划线部分缺失的代码。

注意,只填写缺少的部分,不要填写已有的代码或符号,也不要加任何说明文字。

思路:根据题目要求,读代码,然后先运行一下看缺少什么,然后进行补全即可。

答案:if(v==500)buf[y+1][p1+i-3+l.getWidth()]=sv.charAt(i);else if(l==null&&r==null)buf[y+1][p1+i]=sv.charAt(i);else buf[y+1][p1+i+3]=sv.charAt(i)

 

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(v==500)buf[y+1][p1+i-3+l.getWidth()]=sv.charAt(i);else if(l==null&&r==null)buf[y+1][p1+i]=sv.charAt(i);else buf[y+1][p1+i+3]=sv.charAt(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();		
	}
}
//if(v==500)buf[y+1][p1+i-3+l.getWidth()]=sv.charAt(i);else if(l==null&&r==null)buf[y+1][p1+i]=sv.charAt(i);else buf[y+1][p1+i+3]=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

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

思路:用图保存一下,然后搜进行索。

import java.util.Scanner;

public class Main {

	public static String [][] a;
	public static boolean [][] b;
	public static int n;
	public static int count = 0,min = Integer.MAX_VALUE;
	public static int [][] localtion = {{1,0},{-1,0},{0,1},{0,-1}};
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		a = new String [n][n];
		b = new boolean [n][n];
		int x=0,y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				a[i][j] = in.next();
				if(a[i][j].equals("A")){
					x = i;
					y = j;
				}
			}
		}
		dfs(x,y,0);
		if(min==Integer.MAX_VALUE)
			System.out.println("-1");
		else
			System.out.println(min);
	}
	private static void dfs(int x,int y,int z) {
		for(int i=0;i<4;i++){
			int x1 = x+localtion[i][0];
			int y1 = y+localtion[i][1];
			if(x1<0||x1>=n||y1<0||y1>=n||b[x1][y1]==true)
				continue;
			if(a[x1][y1].equals("A")){
				continue;
			}else if(a[x1][y1].equals("+")){
				if(z==-1||z==0)
					z = 1;
				else
					continue;
			}else if(a[x1][y1].equals("-")){
				if(z==1||z==0)
					z = -1;
				else
					continue;
			}else if(a[x1][y1].equals("B")){
				count++;
				min = min>count?count:min;
				return;
			}
			b[x1][y1]=true;
			count++;
			dfs(x1,y1,z);
			count--;
			b[x1][y1]=false;
		}
	}
}

第5题 表格计算

代码来自CSDN 作者: 萤火虫的微亮 原文:https://blog.csdn.net/weixin_42318538/article/details/89499063

第6题 铺瓷砖

标题:铺瓷砖

为了让蓝桥杯竞赛更顺利的进行,主办方决定给竞赛的机房重新铺放瓷砖。机房可以看成一个n*m的矩形,而这次使用的瓷砖比较特别,有两种形状,如【图1.png】所示。在铺放瓷砖时,可以旋转。
 
主办方想知道,如果使用这两种瓷砖把机房铺满,有多少种方案。

【输入格式】
输入的第一行包含两个整数,分别表示机房两个方向的长度。

【输出格式】
输出一个整数,表示可行的方案数。这个数可能很大,请输出这个数除以65521的余数。

【样例输入1】
4 4
【样例输出1】
2
【样例说明1】
这两种方案如下【图2.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


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萤火的微亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值