第六届蓝桥杯Java A组决赛试题

1.

标题:胡同门牌号


小明家住在一条胡同里。胡同里的门牌号都是连续的正整数,由于历史原因,最小的号码并不是从1开始排的。
有一天小明突然发现了有趣的事情:
如果除去小明家不算,胡同里的其它门牌号加起来,刚好是100!
并且,小明家的门牌号刚好等于胡同里其它住户的个数!


请你根据这些信息,推算小明家的门牌号是多少?


请提交该整数,不要填写任何多余的内容或说明性文字。


运行结果有两个:8和10,但是针对此题,答案到底是8还是10还是8和10,不好判定。以下代码仅供参考。
(1)8 

(2)10

import java.util.*;
public class Main {

    public static void main(String[] args) {
        // write your code here
        for(int i=1;i<20;i++)
        {
            for(int start=1;start<=i;start++)
            {
                int sum=(start+(start+i))*(i+1)/2-i;
                if(sum==100)
                {
                    System.out.println(i);
                }
            }
        }
    }

}
2.

标题:四阶幻方


把1~16的数字填入4x4的方格中,使得行、列以及两个对角线的和都相等,满足这样的特征时称为:四阶幻方。


四阶幻方可能有很多方案。如果固定左上角为1,请计算一共有多少种方案。
比如:
  1  2 15 16
 12 14  3  5
 13  7 10  4
  8 11  6  9


以及:
  1 12 13  8
  2 14  7 11
 15  3 10  6
 16  5  4  9
 
就可以算为两种不同的方案。


请提交左上角固定为1时的所有方案数字,不要填写任何多余内容或说明文字。


答案:416

思路:全排暴力法居然超时了,这不是我认识的蓝桥杯。后面更正为DFS。(跑了4min)

import java.util.*;
public class Main {

    static int count=0;
    public static boolean check(int a[])
    {
        int []r=new int[10];
        r[0]=1+a[0]+a[1]+a[2];
        r[1]=a[3]+a[4]+a[5]+a[6];
        r[2]=a[7]+a[8]+a[9]+a[10];
        r[3]=a[11]+a[12]+a[13]+a[14];
        r[4]=1+a[3]+a[7]+a[11];
        r[5]=a[0]+a[4]+a[8]+a[12];
        r[6]=a[1]+a[5]+a[9]+a[13];
        r[7]=a[2]+a[6]+a[10]+a[14];
        r[8]=1+a[4]+a[9]+a[14];
        r[9]=a[2]+a[5]+a[8]+a[11];

        for(int i=0;i<9;i++)
        {
            for(int j=i+1;j<10;j++)
            {
                if(r[i]==r[j])
                {
                    return false;
                }
            }
        }

        return true;

    }
    public static void main(String[] args) {
        int[] array={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
        allSort(array, 0, array.length-1);
        System.out.println(count);
    }
    static void allSort(int[] array,int begin,int end){

        if(begin==end){
            if(check(array))
            {
                System.out.println(Arrays.toString(array));
                count++;
            }
            return;
        }

        for(int i=begin;i<=end;i++){
            swap(array,begin,i );
            allSort(array, begin+1, end);
            swap(array,begin,i );
        }
    }

    static void swap(int[] array,int a,int b){
        int tem=array[a];
        array[a]=array[b];
        array[b]=tem;
    }


}

import java.util.*;
public class Main {
    private static int count=0;
    public static void main(String args[]){
        int a[]=new int[16];
        a[0]=1;
        for(int i=1;i<16;i++)
            a[i]=0;//初始化
        dfs(a,1);
        System.out.print(count);
    }
    public static void dfs(int a[],int n){
        //填充第n个格子
        if(n==16){
            count++;
        }else{
            for(int i=2;i<=16;i++){
                a[n]=i;
                if(check(a,n))
                    dfs(a,n+1);
            }
        }
    }
    public static boolean check(int a[],int n){
        boolean f=true;
        for(int i=0;i

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; ip2) 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();		
	}
}
    对于上边的测试数据,应该显示出:
     第六届蓝桥杯Java A组决赛试题_第1张图片
    (如有对齐问题,请参考【图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


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


所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。

注意:主类的名字必须是:Main,否则按无效代码处理。

思路:简单DFS

import java.util.*;
public class Main {
    static int n;
    static int map[][];
    static boolean vis[][];
    static int nx,ny,tx,ty;
    static int x[]={0,0,1,-1};
    static int y[]={1,-1,0,0};
    static int res=100000000;

    public static void dfs(int px,int py,int step)
    {
        if(px==tx && py==ty)
        {
            if (step
5.

标题:切开字符串


Pear有一个字符串,不过他希望把它切成两段。
这是一个长度为N(<=10^5)的字符串。
Pear希望选择一个位置,把字符串不重复不遗漏地切成两段,长度分别是t和N-t(这两段都必须非空)。


Pear用如下方式评估切割的方案:
定义“正回文子串”为:长度为奇数的回文子串。
设切成的两段字符串中,前一段中有A个不相同的正回文子串,后一段中有B个不相同的非正回文子串,则该方案的得分为A*B。


注意,后一段中的B表示的是:“...非正回文...”,而不是: “...正回文...”。
那么所有的切割方案中,A*B的最大值是多少呢?


【输入数据】
输入第一行一个正整数N(<=10^5)
接下来一行一个字符串,长度为N。该字符串仅包含小写英文字母。
【输出数据】
一行一个正整数,表示所求的A*B的最大值。
【样例输入】
10
bbaaabcaba
【样例输出】
38
【数据范围】
对于20%的数据,N<=100
对于40%的数据,N<=1000
对于100%的数据,N<=10^5


资源约定:
峰值内存消耗(含虚拟机) < 512M
CPU消耗  < 2000ms




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


所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。

注意:主类的名字必须是:Main,否则按无效代码处理。


思路:暴力法

import java.util.*;
public class Main {

    static String str;
    static String str1;
    static String str2;

    public static boolean is_reT(String s)
    {
        int l=s.length();
        if(l==1)
        {
            return true;
        }
        if(l%2==0)
        {
            return false;
        }

        int i=0;
        while(i!=l-1-i)
        {
            if(s.charAt(i)!=s.charAt(l-1-i))
            {
                return false;
            }
            i++;
        }
        return true;
    }

    public static boolean is_not_reT(String s)
    {
        int l=s.length();
        if(l==1)
        {
            return false;
        }
        if(l%2==0)
        {
            return true;
        }

        int i=0;
        while(i!=l-1-i)
        {
            if(s.charAt(i)!=s.charAt(l-1-i))
            {
                return true;
            }
            i++;
        }
        return false;
    }

    public static int count1(String s)
    {
        int n=s.length();
        Set set = new HashSet<>();
        for(int i=1;i<=n;i++)
        {
            for(int p=0;p<=n-i;p++)
            {
                    String str=s.substring(p,p+i);
                    if(is_reT(str))
                    {
                        set.add(str);
                    }
            }
        }
        return set.size();
    }

    public static int count2(String s)
    {
        int n=s.length();
        Set set = new HashSet<>();
        for(int i=1;i<=n;i++)
        {
            for(int p=0;p<=n-i;p++)
            {
                String str=s.substring(p,p+i);
                if(is_not_reT(str))
                {
                    set.add(str);
                }
            }
        }
        return set.size();
    }

    public static void main(String args[]) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        str=in.next();
        int max=0;

        for(int i=1;imax)
            {
                max=temp;
            }
        }

        System.out.println(max);

    }
}

6.



标题:铺瓷砖


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


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


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


【样例输入1】
4 4
【样例输出1】
2
【样例说明1】
这两种方案如下【图2.png】所示:
 第六届蓝桥杯Java A组决赛试题_第3张图片
【样例输入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,否则按无效代码处理。


思路:应该是先拆分砖块数,如12=3*4=4*3,再分别考察每种情况是否可行,但是题目貌似把旋转对称的方案也算作不同,先占坑。

你可能感兴趣的:(蓝桥杯)