zoukankan      html  css  js  c++  java
  • 2015年第六届蓝桥杯决赛Java本科B组试题解析

    第一题

     1 标题:分机号
     2 
     3 X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:
     4 
     5 751,520,321 都满足要求,而,
     6 766,918,201 就不符合要求。
     7 
     8 现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?
     9 
    10 请直接提交该数字,不要填写任何多余的内容。
     1 public class t1 {
     2     
     3     
     4     private static boolean check(int n) {
     5         
     6         int a = n / 100;
     7         int b = n % 100 / 10;
     8         int c = n % 100 % 10;
     9         
    10         // System.out.println(a + " " + b + " " + c);
    11         
    12         if(a == b || a == c || b == c) {
    13             return false;
    14         }
    15         if(a <= b || b <= c || a <= c) {
    16             return false;
    17         }
    18         
    19         return true;
    20     }
    21     
    22     public static void main(String[] args) {
    23         
    24         int res = 0;
    25         for(int i=210; i<=999; i++) {
    26             
    27             if(check(i)) {
    28                 res++;
    29             }
    30             
    31         }
    32         
    33         System.out.println(res);   // answer: 120
    34         
    35 //        System.out.println(check(918));
    36 //        System.out.println(check(201));
    37 //        System.out.println(check(766));
    38         
    39         
    40     }
    41     
    42 }

    第二题

     1 标题:五星填数
     2 
     3 如【图1.png】的五星图案节点填上数字:1~12,除去7和11。
     4 要求每条直线上数字和相等。
     5 
     6 如图就是恰当的填法。
     7 
     8 请你利用计算机搜索所有可能的填法有多少种。
     9 注意:旋转或镜像后相同的算同一种填法。
    10 
    11 请提交表示方案数目的整数,不要填写任何其它内容。

    思路:全排列10个数然后依次进行检查,注意最后结果要除10(去重)

     1 public class t2 {
     2     
     3     public static int[] arr = {1, 2, 3, 4, 5, 6, 8, 9, 10, 12};
     4     public static int res;
     5     
     6     public static boolean check() {
     7         int r1 = arr[0] + arr[2] + arr[5] + arr[8];
     8         int r2 = arr[0] + arr[3] + arr[6] + arr[9];
     9         int r3 = arr[1] + arr[2] + arr[3] + arr[4];
    10         int r4 = arr[1] + arr[5] + arr[7] + arr[9];
    11         int r5 = arr[4] + arr[6] + arr[7] + arr[8];
    12         if(r1 == r2 && r2 == r3 && r3 == r4 && r4 == r5) {
    13             return true;
    14         }
    15         
    16         return false;
    17     }
    18     
    19     public static void f(int x) {
    20         
    21         if(x==10) {
    22             if(check()) {
    23                 res += 1;
    24             }
    25             return;
    26         }     
    27         
    28         for(int i=x; i<10; i++) {
    29             int temp = arr[x];
    30             arr[x] = arr[i];
    31             arr[i] = temp;
    32             f(x+1);
    33             temp = arr[x];
    34             arr[x] = arr[i];
    35             arr[i] = temp;
    36         }
    37         
    38     }
    39     
    40     public static void main(String[] args) {
    41         
    42         f(0);
    43         System.out.println(res);
    44         System.out.println(res/10);
    45         
    46         // answer: 12
    47     }
    48     
    49     
    50     
    51 }

    第三题  

      1 标题:显示二叉树
      2 
      3 排序二叉树的特征是:
      4 某个节点的左子树的所有节点值都不大于本节点值。
      5 某个节点的右子树的所有节点值都不小于本节点值。
      6 
      7 为了能形象地观察二叉树的建立过程,小明写了一段程序来显示出二叉树的结构来。
      8 
      9 
     10 class BiTree
     11 {
     12     private int v;
     13     private BiTree l;
     14     private BiTree r;
     15     
     16     public BiTree(int v){
     17         this.v = v;
     18     }
     19     
     20     public void add(BiTree the){
     21         if(the.v < v){
     22             if(l==null) l = the;
     23             else l.add(the);
     24         }
     25         else{
     26             if(r==null) r = the;
     27             else r.add(the);
     28         }
     29     }
     30     
     31     public int getHeight(){
     32         int h = 2;
     33         int hl = l==null? 0 : l.getHeight();
     34         int hr = r==null? 0 : r.getHeight();
     35         return h + Math.max(hl,hr);
     36     }
     37     
     38     public int getWidth(){
     39         int w = (""+v).length();
     40         if(l!=null) w += l.getWidth();
     41         if(r!=null) w += r.getWidth();
     42         return w;
     43     }
     44     
     45     public void show(){
     46         char[][] buf = new char[getHeight()][getWidth()];
     47         printInBuf(buf, 0, 0);
     48         showBuf(buf);
     49     }
     50     
     51     private void showBuf(char[][] x){
     52         for(int i=0; i<x.length; i++){
     53             for(int j=0; j<x[i].length; j++)
     54                 System.out.print(x[i][j]==0? ' ':x[i][j]);
     55             System.out.println();
     56         }
     57     }
     58     
     59     private void printInBuf(char[][] buf, int x, int y){
     60         String sv = "" + v;
     61         
     62         int p1 = l==null? x : l.getRootPos(x);
     63         int p2 = getRootPos(x);
     64         int p3 = r==null? p2 : r.getRootPos(p2+sv.length());
     65         
     66         buf[y][p2] = '|';
     67         for(int i=p1; i<=p3; i++) buf[y+1][i]='-';
     68         for(int i=0; i<sv.length(); i++) ________________________________;  //填空位置
     69         if(p1<p2) buf[y+1][p1] = '/';
     70         if(p3>p2) buf[y+1][p3] = '\';
     71         
     72         if(l!=null) l.printInBuf(buf,x,y+2);
     73         if(r!=null) r.printInBuf(buf,p2+sv.length(),y+2);
     74     }
     75     
     76     private int getRootPos(int x){
     77         return l==null? x : x + l.getWidth();
     78     }
     79 }
     80 
     81 public class Main
     82 {
     83     public static void main(String[] args)
     84     {        
     85         BiTree tree = new BiTree(500);
     86         tree.add(new BiTree(200));
     87         tree.add(new BiTree(509));
     88         tree.add(new BiTree(100));
     89         tree.add(new BiTree(250));
     90         tree.add(new BiTree(507));
     91         tree.add(new BiTree(600));
     92         tree.add(new BiTree(650));
     93         tree.add(new BiTree(450));
     94         tree.add(new BiTree(510));
     95         tree.add(new BiTree(440));
     96         tree.add(new BiTree(220));        
     97         tree.show();        
     98     }
     99 }
    100 
    101 对于上边的测试数据,应该显示出:
    102                   |
    103    /--------------500---
    104    |                    |
    105 /--200---           /--509---
    106 |        |           |        |
    107 100   /--250---     507   /--600
    108       |        |           |     |
    109       220   /--450         510   650
    110             |
    111             440
    112 
    113 (如有对齐问题,请参考【图1.png】)
    114 
    115 请分析程序逻辑,填写划线部分缺失的代码。
    116 
    117 注意,只填写缺少的部分,不要填写已有的代码或符号,也不要加任何说明文字。

    答案:

    buf[y+1][i+p2] = sv.charAt(i)

  • 相关阅读:
    QML控件设置style样式
    QML:ListElement: cannot use script for property value
    内存更换乌龙事件
    服务器的寿命到底有多长
    这些年渠道管理系统上发生的那些事
    IT囧事之误删测试环境的生产库
    旁观一个软件产品的终结
    一根网线引发的血案
    论代码规范命名的重要性
    服务器故障之内存损坏
  • 原文地址:https://www.cnblogs.com/wyb666/p/10891219.html
Copyright © 2011-2022 走看看