zoukankan      html  css  js  c++  java
  • 第九届蓝桥杯JavaB组国(决)赛真题

    解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论

    题目1、三角形面积

    已知三角形三个顶点在直角坐标系下的坐标分别为:
    (2.3, 2.5)
    (6.4, 3.1)
    (5.1, 7.2)

    求该三角形的面积。

    注意,要提交的是一个小数形式表示的浮点数。
    要求精确到小数后3位,如不足3位,需要补零。

    // 海伦公式
    public class Main {
     
    	public static void main(String[] args) {
    		double a = Math.sqrt((6.4 - 2.3) * (6.4 - 2.3) + (3.1 - 2.5) * (3.1 - 2.5));
    		double b = Math.sqrt((5.1 - 2.3) * (5.1 - 2.3) + (7.2 - 2.5) * (7.2 - 2.5));
    		double c = Math.sqrt((6.4 - 5.1) * (6.4 - 5.1) + (7.2 - 3.1) * (7.2 - 3.1));
    		double q = (a + b + c) / 2.0;
    		double area = Math.sqrt(q * (q - a) * (q - b) * (q - c));
    		System.out.println(area);
    	}
     
    }
    
    题目2、最大乘积

    把 1~9 这9个数字分成两组,中间插入乘号,
    有的时候,它们的乘积也只包含1~9这9个数字,而且每个数字只出现1次。

    比如:
    984672 * 351 = 345619872
    98751 * 3462 = 341875962
    9 * 87146325 = 784316925

    符合这种规律的算式还有很多,请你计算在所有这些算式中,乘积最大是多少?

    注意,需要提交的是一个整数,表示那个最大的积,不要填写任何多余的内容。
    (只提交乘积,不要提交整个算式)

    // 答案:839542176
    public class Main {
    	static int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    	static int ans = 0;
     
    	public static void main(String[] args) {
    		f(a.length - 1);
    		System.out.println(ans);
    	}
     
    	public static void f(int start) {
    		if (start >= 0 && start <= 7) {
    			check(start);
    		}
     
    		for (int i = start; i >= 0; i--) {
    			{
    				int temp = a[i];
    				a[i] = a[start];
    				a[start] = temp;
    			}
    			f(start - 1);
    			{
    				int temp = a[i];
    				a[i] = a[start];
    				a[start] = temp;
    			}
    		}
    	}
     
    	public static boolean check(int start) {
    		String s1 = "";
    		for (int i = 0; i <= start; i++) {
    			s1 += a[i];
    		}
    		String s2 = "";
    		for (int i = start + 1; i < a.length; i++) {
    			s2 += a[i];
    		}
    		int num1 = Integer.parseInt(s1);
    		int num2 = Integer.parseInt(s2);
    		int sum = num1 * num2;
    		if (sum < 830000000 || sum > 987654321)
    			return false;
    		if (!isOk(sum))
    			return false;
    		if (sum > ans)
    			ans = sum;
    		return true;
    	}
     
    	public static boolean isOk(int num) {
    		String s = "" + num;
    		for (int i = 1; i <= 9; i++) {
    			if (s.indexOf(i + "") == -1)
    				return false;
    		}
    		return true;
    	}
    }
    
    题目3、全排列

    对于某个串,比如:“1234”,求它的所有全排列。
    并且要求这些全排列一定要按照字母的升序排列。
    对于“1234”,应该输出(一共4!=24行):
    1234
    1243
    1324
    1342
    1423
    1432
    2134
    2143
    2314
    2341
    2413
    2431
    3124
    3142
    3214
    3241
    3412
    3421
    4123
    4132
    4213
    4231
    4312
    4321

    下面是实现程序,请仔细分析程序逻辑,并填写划线部分缺少的代码。

    // 轮换前k个,再递归处理

    import java.util.*;
    public class A
    {
    	static void permu(char[] data, int cur){
    		if(cur==data.length-1){
    			System.out.println(new String(data));
    			return;
    		}
    		
    		for(int i=cur; i<data.length; i++){
    			char tmp = data[i]; 
    			for(int j=i-1; j>=cur; j--) data[j+1] = data[j];
    			data[cur] = tmp;			
     
    			permu(data, cur+1);			
     
    			tmp = data[cur]; 
    			__________________________________________ ;
    			data[i] = tmp;			
    		}
    	}
    	
    	static void permu(String x){
    		permu(x.toCharArray(),0);
    	}
    	
    	public static void main(String[] args){
    		permu("1234");
    	}
    }
    
    // 根据已有的代码,那地方就是要回溯;根据for循环开始的交换方式还有上下两句代码,就可以得到答案了。
     
    for(int j = cur; j < i; j++) data[j] = data[j + 1];
    
    题目4、整理玩具

    小明有一套玩具,一共包含NxM个部件。这些部件摆放在一个包含NxM个小格子的玩具盒中,每个小格子中恰好摆放一个部件。

    每一个部件上标记有一个0~9的整数,有可能有多个部件标记相同的整数。

    小明对玩具的摆放有特殊的要求:标记相同整数的部件必须摆在一起,组成一个矩形形状。

    如以下摆放是满足要求的:

    00022
    00033
    44444

    12244
    12244
    12233

    01234
    56789

    以下摆放不满足要求:

    11122
    11122
    33311

    111111
    122221
    122221
    111111

    11122
    11113
    33333

    给出一种摆放方式,请你判断是否符合小明的要求。

    输入

    输入包含多组数据。
    第一行包含一个整数T,代表数据组数。 (1 <= T <= 10)
    以下包含T组数据。
    每组数据第一行包含两个整数N和M。 (1 <= N, M <= 10)
    以下包含N行M列的矩阵,代表摆放方式。

    输出

    对于每组数据,输出YES或者NO代表是否符合小明的要求。

    【样例输入】
    3
    3 5
    00022
    00033
    44444
    3 5
    11122
    11122
    33311
    2 5
    01234
    56789

    【样例输出】
    YES
    NO
    YES

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

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

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

    PS:

    分析
    一开始想着用连通性做,发现挺麻烦的,后来主要思考矩形这个条件限制,想到了一个非常巧妙的做法。
    既然是矩形,那就必然有固定的长和宽,那么我们为每一种玩具设置矩形的边界值,用maxX、maxY、minX、minY来确定矩形的范围。先假设所有的玩具都是符合在一个矩形中这个条件。读取数据不断更新更新矩形的边界,最后比较每种矩形是否有重叠的部分,如果有,说明存在不在一个矩形内的玩具,则是NO。
    注:矩形重叠的充分必要条件是:横轴上矩形A的右边界值大于等于矩形B的左边界值且矩形A的左边界值小于等于矩形B的右边界值,同时竖轴上矩形A的上边界值大于等于矩形B的下边界值且矩形A的下边界值小于等于矩形B的上边界值。

    import java.util.Scanner;;
    
    class Fanwei {
    	int minX;
    	int maxX;
    	int minY;
    	int maxY;
    	Fanwei() {
    		minX = minY = maxX = maxY = -10;
    	}
    }
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner in = new Scanner(System.in);
    		int T = in.nextInt();
    		for (int i = 0; i < T; i++)
    		{
    			boolean res = true;
    			Fanwei[] fanwei = new Fanwei[10];
    			for (int j = 0; j < 10; j++)
    				fanwei[j] = new Fanwei();
    			int N = in.nextInt();
    			int M = in.nextInt();
    			in.nextLine();
    			int[][] gezi = new int[N][M];
    			for (int j = 0; j < N; j++)
    			{
    				char[] str = in.nextLine().toCharArray();
    				for (int k = 0; k < M; k++)
    				{
    					gezi[j][k] = str[k]-48;
    				}
    			}
    			for (int j = 0; j < N; j++)
    			{
    				for (int k = 0; k < M; k++)
    				{
    					int tmp = gezi[j][k];
    					if (fanwei[tmp].minX == -10 || fanwei[tmp].minX > j)
    						fanwei[tmp].minX = j;
    					if (fanwei[tmp].maxX == -10 || fanwei[tmp].maxX < j)
    						fanwei[tmp].maxX = j;
    					if (fanwei[tmp].minY == -10 || fanwei[tmp].minY > k)
    						fanwei[tmp].minY = k;
    					if (fanwei[tmp].maxY == -10 || fanwei[tmp].maxY < k)
    						fanwei[tmp].maxY = k;
    				}
    			}
    			OUT:
    			for (int j = 0; j < 9; j++)
    			{
    				if (fanwei[j].minX == -10)
    					continue;
    				for (int k = j+1; k < 10; k++)
    				{
    					if (fanwei[k].minX == -10)
    						continue;
    					if (fanwei[j].maxX >= fanwei[k].minX && fanwei[j].minX <= fanwei[k].maxX)
    					{
    						if (fanwei[j].maxY >= fanwei[k].minY && fanwei[j].minY <= fanwei[k].maxY)
    						{
    							res = false;
    							break OUT;
    						}
    					}
    				}
    			}
    			if (res)
    				System.out.println("YES");
    			else
    				System.out.println("NO");
    		}
    	}
    }
    
    
    题目5、版本分支
    第五题:版本分支
    小明负责维护公司一个奇怪的项目。这个项目的代码一直在不断分支(branch)但是从未发生过合并(merge)。
    现在这个项目的代码一共有N个版本,编号1~N,其中1号版本是最初的版本。
    除了1号版本之外,其他版本的代码都恰好有一个直接的父版本;即这N个版本形成了一棵以1为根的树形结构。  
    
    如下图就是一个可能的版本树:
    
        1
       / 
      2   3
      |  / 
      5 4   6
    
    现在小明需要经常检查版本x是不是版本y的祖先版本。你能帮助小明吗?
    
    输入
    ----
    第一行包含两个整数N和Q,代表版本总数和查询总数。  
    以下N-1行,每行包含2个整数u和v,代表版本u是版本v的直接父版本。  
    再之后Q行,每行包含2个整数x和y,代表询问版本x是不是版本y的祖先版本。  
    
    对于30%的数据,1 <= N <= 1000  1 <= Q <= 1000  
    对于100%的数据,1 <= N <= 100000  1 <= Q <= 100000  
    
    输出
    ----
    对于每个询问,输出YES或NO代表x是否是y的祖先。  
    
    【样例输入】
    6 5
    1 2
    1 3
    2 5
    3 6
    3 4
    1 1
    1 4
    2 6
    5 2
    6 4
    
    【样例输出】
    YES
    YES
    NO
    NO
    NO
    
    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms
    
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    
    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    不要使用package语句。不要使用jdk1.7及以上版本的特性。
    主类的名字必须是:Main,否则按无效代码处理。
    
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.math.BigInteger;
    import java.util.*;
     
    public class MainB {
        public static InputReader in = new InputReader(new BufferedInputStream(System.in));
        public static PrintWriter out = new PrintWriter(System.out);
        public static int n, q, u, v, x, y;
        public static ArrayList<Integer>[] child = new ArrayList[100010];
        public static ArrayList<Integer>[] next = new ArrayList[100010];
     
        public static void main(String[] args) {
            n = in.nextInt();
            q = in.nextInt();
            for (int i = 1; i <= n; i++) {
                next[i] = new ArrayList<>();
                child[i] = new ArrayList<>();
            }
            for (int i = 1; i < n; i++) {
                u = in.nextInt();
                v = in.nextInt();
                next[u].add(v);
            }
            child[1] = getChild(1);
            while (q-- > 0) {
                x =in.nextInt();
                y = in.nextInt();
                if (child[x].contains(y)) {
                    out.println("YES");
                    out.flush();
                } else {
                    out.println("NO");
                    out.flush();
                }
            }
            out.close();
        }
     
        static ArrayList<Integer> getChild(int root) {
            int len = next[root].size();
            for (int i = 0; i < len; i++)
                child[root].addAll(getChild(next[root].get(i)));
            child[root].add(root);
            return child[root];
        }
     
        static class InputReader {
            public BufferedReader reader;
            public StringTokenizer tokenizer;
     
            public InputReader(InputStream stream) {
                reader = new BufferedReader(new InputStreamReader(stream), 32768);
                tokenizer = null;
            }
     
            public String next() {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    try {
                        tokenizer = new StringTokenizer(reader.readLine());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                return tokenizer.nextToken();
            }
     
            public String nextLine() {
                String str = null;
                try {
                    str = reader.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str;
            }
     
            public int nextInt() {
                return Integer.parseInt(next());
            }
     
            public long nextLong() {
                return Long.parseLong(next());
            }
     
            public Double nextDouble() {
                return Double.parseDouble(next());
            }
     
            public BigInteger nextBigInteger() {
                return new BigInteger(next());
            }
     
        }
    }
    
    题目6、三角形面积
    小明最近在玩一款游戏。对游戏中的防御力很感兴趣。
    我们认为直接影响防御的参数为“防御性能”,记作d,而面板上有两个防御值A和B,与d成对数关系,A=2^d,B=3^d(注意任何时候上式都成立)。
    在游戏过程中,可能有一些道具把防御值A增加一个值,有另一些道具把防御值B增加一个值。
    现在小明身上有n1个道具增加A的值和n2个道具增加B的值,增加量已知。
    
    现在已知第i次使用的道具是增加A还是增加B的值,但具体使用那个道具是不确定的,请找到一个字典序最小的使用道具的方式,使得最终的防御性能最大。
    
    初始时防御性能为0,即d=0,所以A=B=1。
    
    【输入格式】
    输入的第一行包含两个数n1,n2,空格分隔。
    第二行n1个数,表示增加A值的那些道具的增加量。
    第三行n2个数,表示增加B值的那些道具的增加量。
    第四行一个长度为n1+n2的字符串,由0和1组成,表示道具的使用顺序。0表示使用增加A值的道具,1表示使用增加B值的道具。输入数据保证恰好有n1个0,n2个1。
    
    【输出格式】
    对于每组数据,输出n1+n2+1行,前n1+n2行按顺序输出道具的使用情况,若使用增加A值的道具,输出Ax,x为道具在该类道具中的编号(从1开始)。若使用增加B值的道具则输出Bx。最后一行输出一个大写字母E。
    
    【样例输入1】
    1 2
    4
    2 8
    101
    
    【样例输出1】
    B2
    A1
    B1
    E
    
    【样例输入2】
    3 0
    7 11 13
    
    000
    
    【样例输出2】
    A1
    A2
    A3
    E
    
    【样例说明】
    对于第一组测试数据,操作过程如下:
    操作  d         A              B
    初始  0            1              1
    B2    2         4              9
    A1    3            8              27
    B1   log3(29)   2^(log3(29))   29
    
    可以证明,这个值是最大的。
    对于第二组测试数据,可见无论用什么顺序,A最后总为32,即d总为5,B总为243。 
    
    【数据规模】
    对于20%的数据,字符串长度<=10000;
    对于70%的数据,字符串长度<=200000;
    对于100%的数据,字符串长度<=2000000,输入的每个增加值不超过2^30。
    
    
    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms
    
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    
    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    不要使用package语句。不要使用jdk1.7及以上版本的特性。
    主类的名字必须是:Main,否则按无效代码处理。
    
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.math.BigInteger;
    import java.util.*;
     
    public class MainB {
        public static InputReader in = new InputReader(new BufferedInputStream(System.in));
        public static PrintWriter out = new PrintWriter(System.out);
        public static int n1, n2, d, a, b, len, ka, kb, k;
        public static String s;
        public static A[] ai = new A[2000010];
        public static B[] bi = new B[2000010];
        public static int[] order;
     
        public static void main(String[] args) {
            d = 0;
            a = 1;
            b = 1;
            n1 = in.nextInt();
            n2 = in.nextInt();
            if (n1 == 0) s = in.nextLine();//吸取空行
            for (int i = 1; i <= n1; i++) {
                ai[i] = new A();
                ai[i].id = i;
                ai[i].value = in.nextInt();
            }
            if (n2 == 0) s = in.nextLine();
            for (int i = 1; i <= n2; i++) {
                bi[i] = new B();
                bi[i].id = i;
                bi[i].value = in.nextInt();
            }
            s = in.nextLine();
            Arrays.sort(ai, 1, n1+1);
            Arrays.sort(bi, 1, n2+1);
            len = s.length();
            ka = 1;
            kb = 1;
            for (int i = 0; i < len; i++) {
                if (s.charAt(i) == '0') {
                    if (s.charAt(i) == '1') {
                        out.println("A" + ai[ka++].id);
                        out.flush();
                    } else {//出现连续的0
                        order = new int[len-i+5];
                        k = 0;
                        order[k++] = ai[ka++].id;//将这一段连续的'0'对应的id存在一个数组里
                        int j = i + 1;
                        for (j = i+1; j < len; j++) {
                            if (s.charAt(j) != '0') break;
                            order[k++] = ai[ka++].id;
                        }
                        Arrays.sort(order, 0, k);//按id从小到大排(即字典序从小到大)
                        i = j - 1;//调整i,使i下一次循环是从后面第一个'1'处开始
                        for (j = 0; j < k; j++) {
                            out.println("A" + order[j]);
                            out.flush();
                        }
                    }
                } else {
                    if (s.charAt(i) == '0') {
                        out.println("B" + bi[kb++].id);
                        out.flush();
                    } else {//出现连续的1
                        order = new int[len-i+5];
                        k = 0;
                        order[k++] = bi[kb++].id;//将这一段连续的'1'对应的id存在一个数组里
                        int j = i + 1;
                        for (j = i+1; j < len; j++) {
                            if (s.charAt(j) != '1') break;
                            order[k++] = bi[kb++].id;
                        }
                        Arrays.sort(order, 0, k);//按id从小到大排(即字典序从小到大)
                        i = j - 1;//调整i,使i下一次循环是从后面第一个'0'处开始
                        for (j = 0; j < k; j++) {
                            out.println("B" + order[j]);
                            out.flush();
                        }
                    }
                }
            }
            out.println("E");
            out.flush();
            out.close();
        }
     
        static class A implements Comparable<A>{
            int id, value;
     
            @Override
            public int compareTo(A o) {
                if (o.value - this.value != 0) {
                    return o.value - this.value;
                } else {
                    return this.id - o.id;
                }
            }
        }
     
        static class B implements Comparable<B>{
            int id, value;
     
            @Override
            public int compareTo(B o) {
                if (o.value - this.value != 0) {
                    return o.value - this.value;
                } else {
                    return this.id - o.id;
                }
            }
        }
     
        static class InputReader {
            public BufferedReader reader;
            public StringTokenizer tokenizer;
     
            public InputReader(InputStream stream) {
                reader = new BufferedReader(new InputStreamReader(stream), 32768);
                tokenizer = null;
            }
     
            public String next() {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    try {
                        tokenizer = new StringTokenizer(reader.readLine());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                return tokenizer.nextToken();
            }
     
            public String nextLine() {
                String str = null;
                try {
                    str = reader.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str;
            }
     
            public int nextInt() {
                return Integer.parseInt(next());
            }
     
            public long nextLong() {
                return Long.parseLong(next());
            }
     
            public Double nextDouble() {
                return Double.parseDouble(next());
            }
     
            public BigInteger nextBigInteger() {
                return new BigInteger(next());
            }
     
        }
    }
    
  • 相关阅读:
    BZOJ 3157 &数学乱搞...
    BZOJ 2654 & 玄学二分+MST
    概率与期望的一些问题整理
    BZOJ1024&丝帛搜索
    BZOJ 1260&UVa 4394 区间DP
    hdu Atlantis(线段树的面积并)
    hdu Stars in Your Window(线段树,涉及扫描线建立)
    hdu Potted Flower(线段树,求区间的最值)
    hdu Frequent values (线段树 求最值)
    hdu Stars(树状数组)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078629.html
Copyright © 2011-2022 走看看