第七届(16年)蓝桥杯java B组决赛真题

1、愤怒小鸟(填空)
X星球愤怒的小鸟喜欢撞火车!

一根平直的铁轨上两火车间相距 1000 米
两火车 (不妨称A和B) 以时速 10米/秒 相对行驶。

愤怒的小鸟从A车出发,时速50米/秒,撞向B车,
然后返回去撞A车,再返回去撞B车,如此往复....
两火车在相距1米处停车。

问:这期间愤怒的小鸟撞 B 车多少次?

注意:需要提交的是一个整数(表示撞B车的次数),不要填写任何其它内容。

 1  public class One {
 2     //A、B的位置a、b的大小相对于A的起点而言,小鸟的速度为50,A、B的速度都为10
 3     public static double sum=0;//sum用于鸟和B在规定期间相撞的次数
 4     public static void funb(double a,double b){//在路程a、b间鸟由A撞向B
 5         if(b-a<=1){
 6             System.out.println(sum);
 7             return;
 8         }
 9         double time=(b-a)/(50+10);//距离为a、b期间鸟和B相撞的时的时间
10         a=a+10*time;//撞后A的位置
11         b=b-10*time;//撞后B的位置
12         sum++;
13         funa(a,b);//鸟和B相撞后鸟转向A而行
14         return;
15     }
16     
17     public static void funa(double a,double b){//鸟由B向A飞行
18         if(b-a<1){//若鸟和B撞完后AB距离小于1,则sum--
19             sum--;
20             System.out.println(sum);
21         }
22         double time=(b-a)/(50+10);
23         a=a+10*time;//撞后A的位置
24         b=b-10*time;//撞后B的位置
25         funb(a,b);
26         return;
27     }
28     public static void main(String args[]){
29         double a=0,b=1000;
30         funb(a,b);
31     }
32 }
愤怒的小鸟(两个函数间递归)

答案为:9

2、反幻方(填空)
我国古籍很早就记载着

2 9 4
7 5 3
6 1 8
这是一个三阶幻方。每行每列以及对角线上的数字相加都相等。

下面考虑一个相反的问题。
可不可以用 1~9 的数字填入九宫格。
使得:每行每列每个对角线上的数字和都互不相等呢?

这应该能做到。
比如:
9 1 2
8 4 3
7 5 6

你的任务是搜索所有的三阶反幻方。并统计出一共有多少种。
旋转或镜像算同一种。


比如:
9 1 2
8 4 3
7 5 6

7 8 9
5 4 1
6 3 2

2 1 9
3 4 8
6 5 7
等都算作同一种情况。

请提交三阶反幻方一共多少种。这是一个整数,不要填写任何多余内容。

 1 public class Main {
 2     public static int sum = 0;// 计数器
 3 
 4     public static void fun(int a[], int n) {
 5         if (n == 9) {
 6             text(a);
 7             return;
 8         } else {
 9             for (int i = n; i < 9; i++) { // n后面的
10                 int temp = a[i];
11                 a[i] = a[n];
12                 a[n] = temp;
13                 fun(a, n + 1);
14                 int tamp = a[i];
15                 a[i] = a[n];
16                 a[n] = tamp;
17             }
18         }
19     }
20 
21     public static void text(int a[]) {
22         int l1 = a[0] + a[1] + a[2];
23         int l2 = a[3] + a[4] + a[5];
24         int l3 = a[6] + a[7] + a[8];
25         int s1 = a[0] + a[3] + a[6];
26         int s2 = a[1] + a[4] + a[7];
27         int s3 = a[2] + a[5] + a[8];
28         int x1 = a[0] + a[4] + a[8];
29         int x2 = a[2] + a[4] + a[6];
30         if (l1 != l2 && l1 != l3 && l1 != s1 && l1 != s2 && l1 != s3 && l1 != x1 && l1 != x2)
31             if (l2 != l3 && l2 != s1 && l2 != s2 && l2 != s3 && l2 != x1 && l2 != x2)
32                 if (l3 != s1 && l3 != s2 && l3 != s3 && l3 != x1 && l3 != x2)
33                     if (s1 != s2 && s1 != s3 && s1 != x1 && s1 != x2)
34                         if (s2 != s3 && s2 != x1 && s2 != x2)
35                             if (s3 != x1 && s3 != x2)
36                                 if (x1 != x2)
37                                     sum++;
38     }
39     public static void main(String[] args){  
40         int a[]={1,2,3,4,5,6,7,8,9};  
41         fun(a,0);  
42         System.out.print(sum/8);  
43     }
44 }
三阶反幻方(递归)

答案为:3120


3、打靶(填空)

小明参加X星球的打靶比赛。
比赛使用电子感应计分系统。其中有一局,小明得了96分。

这局小明共打了6发子弹,没有脱靶。
但望远镜看过去,只有3个弹孔。
显然,有些子弹准确地穿过了前边的弹孔。

不同环数得分是这样设置的:
1,2,3,5,10,20,25,50

那么小明的6发子弹得分都是多少呢?有哪些可能情况呢?

下面的程序解决了这个问题。
仔细阅读分析代码,填写划线部分缺失的内容。

public class Main
{    
    static void f(int[] ta, int[] da, int k, int ho, int bu, int sc)
    {
        if(ho<0 || bu<0 || sc<0) return;
        if(k==ta.length){
            if(ho>0 || bu>0 || sc>0) return;
            for(int i=0; i<da.length; i++){
                for(int j=0; j<da[i]; j++)
                    System.out.print(ta[i] + " ");
            }
            System.out.println();
            return;
        }
        
        for(int i=0; i<=bu; i++){
            da[k] = i;
            f(ta, da, k+1,  __________________ , bu-i, sc-ta[k]*i);   // 填空位置
        }
        
        da[k] = 0;
    }
    
    public static void main(String[] args)
    {
        int[] ta = {1,2,3,5,10,20,25,50};
        int[] da = new int[8];
        f(ta, da, 0, 3, 6, 96);
    }
}

注意:只填写划线处缺少的内容,不要填写已有的代码或符号,也不要填写任何解释说明文字等。

 1 public class Main
 2 {    
 3     static void f(int[] ta, int[] da, int k, int ho, int bu, int sc)
 4     {
 5         if(ho<0 || bu<0 || sc<0) return;
 6         if(k==ta.length){
 7             if(ho>0 || bu>0 || sc>0) return;
 8             for(int i=0; i<da.length; i++){
 9                 for(int j=0; j<da[i]; j++) 
10                     System.out.print(ta[i] + " ");
11             }
12             System.out.println();
13             return;
14         }
15         
16         for(int i=0; i<=bu; i++){
17             da[k] = i;
18             f(ta, da, k+1,  ho-1%(i+1) , bu-i, sc-ta[k]*i);   // 填空位置
19         }
20         
21         da[k] = 0;
22     }
23     
24     public static void main(String[] args)
25     {
26         int[] ta = {1,2,3,5,10,20,25,50};
27         int[] da = new int[8];
28         f(ta, da, 0, 3, 6, 96);
29     }
30 }
打靶(递归)

答案(划线代码):ho-1%(i+1)

或:ho-da[k]/Math.max(da[k],1)

4、

原文地址:https://www.cnblogs.com/qinmeizhen/p/6846610.html