2012年第三届蓝桥杯Java本科组省赛试题解析

题目地址:https://wenku.baidu.com/view/326f7b4be518964bcf847c96.html?rec_flag=default    => 百度文档

题目及解析如下:

题目大致介绍:

第一题到第四题是结果填空,方法不限只要得到最后结果就行

第五题到第七题是代码填空,要求在指定的位置处填代码

第八题到第十题是编程题,要求编程解决问题

第一题

第一题很简单,就是个签到题,详情如下:

 1 // 黄金分割数
 2 
 3 public class t1 {
 4     
 5     public static void main(String[] args) {
 6         
 7         int a = 1;
 8         int b = 3;
 9         
10         for(int i=0; i<=50; i++) {
11             int temp = a + b;
12             double s = (double)b/temp;
13             System.out.println(b + " " + temp + " " + s);
14             a = b;
15             b = temp;
16         }
17         
18     }
19     
20     // answer: 5778 9349 0.6180340143330838
21     // 最后提交的答案: 5778 9349
22     // 注: 为了求稳  算出答案后可以用电脑中带的计算器验算一下
23     
24 }

第二题

思路:每次都是所有人平分一瓶酒,然后船长喝了四次,并且正好喝了一瓶

 1 // 海盗比酒量
 2 
 3 public class t2 {
 4 
 5     public static void main(String[] args) {
 6 
 7         for (int m = 1; m <= 20; m++) {
 8             for (int n = 1; n < m; n++) {
 9                 for (int p = 1; p < n; p++) {
10                     for (int q = 1; q < p; q++) {
11                         if (n * p * q + m * p * q + m * n * q + m * n * p == m * n * p * q) {
12                             System.out.println(m + "," + n + "," + p + "," + q + "," + 0);
13                         }
14                     }
15                 }
16             }
17         }
18 
19     }
20 
21 }

第三题

1个圆盘移动1次,2个圆盘移动3次,3个圆盘移动7次,n个圆盘就是移动2^n-1次,64个圆盘就是移动2^64-1次

写代码计算如下:

 1 import java.math.BigInteger;
 2 
 3 public class t3 {
 4 
 5     public static void f(int n) {
 6         BigInteger a = new BigInteger("1");
 7         for (int i = 1; i <= n; i++) {
 8             a = a.multiply(new BigInteger("2"));
 9         }
10 
11         a = a.subtract(new BigInteger("1"));
12         System.out.println(a.toString());
13     }
14     
15     public static void f2(int n) {
16         // 
17         long a = 1;
18         for (int i = 1; i <= n; i++) {
19             a = a * 2;
20         }
21 
22         System.out.println(a - 1);
23     }
24     
25     public static void main(String[] args) {
26 
27         f(1);    // 1    
28         f(2);    // 3
29         f(3);    // 7
30         
31         f(64);    // 18446744073709551615
32         
33         f2(63);    // 将这个答案*2+1就是最后的答案(也就是f(64))
34         f2(64);    // 爆了、、、
35     }
36 
37 }

第四题

比较简单的递归,代码如下:

 1 public class t4 {
 2 
 3     public static void f(int score, int n, String str) {
 4         if (n == 10 && score == 100) {
 5             System.out.println(str);
 6             return;
 7         }
 8         if (n == 10) {
 9             return;
10         }
11 
12         f(score * 2, n + 1, str + "1"); // 答对
13         f(score - (n + 1), n + 1, str + "0"); // 答错
14 
15     }
16 
17     public static void main(String[] args) {
18 
19         f(10, 0, "");
20         
21         // answer:
22         // 1011010000
23         // 0111010000
24         // 0010110011
25 
26 
27     }
28 
29 }

第五题

常规题,代码如下:

 1 public class t5 {
 2     
 3     public static int getFirstNum(String s) {
 4         if(s == null || s.length() == 0) {
 5             return -1;
 6         }
 7         char c = s.charAt(0);
 8         if(c>='0' && c<='9') {
 9             return c - '0';
10         }
11         return getFirstNum(s.substring(1));
12     }
13     
14     public static void main(String[] args) {
15         
16         System.out.println(getFirstNum("abc24us43"));
17         System.out.println(getFirstNum("82445adb5"));
18         System.out.println(getFirstNum("ab"));
19         
20     }
21     
22 }

第六题

常规数学问题,代码如下:

 1 public class t6 {
 2 
 3     public static void main(String[] args)
 4     {
 5         System.out.println("标准 " + Math.PI);
 6     
 7         double a = 1;
 8         int n = 6;
 9     
10         for(int i=0; i<10; i++){    // 可以把n改成100 看更多的结果
11             double b = Math.sqrt(1-(a/2)*(a/2));
12             a = Math.sqrt((1-b)*(1-b) + (a/2)*(a/2));
13         
14             n = 2 * n; //填空
15         
16             System.out.println(n + "  " + a*n/2); // 填空
17         }
18     }
19 
20 }

第七题

常规递归,不断尝试,从而写出如下代码:

 1 import java.util.*;
 2 
 3 public class t7 {
 4     
 5     public static List<Integer> max5(List<Integer> lst)
 6     {
 7         if(lst.size()<=5) return lst;
 8     
 9         int a = lst.remove(0);  // 填空
10         List<Integer> b = max5(lst);
11     
12         for(int i=0; i<b.size(); i++){
13             int t = b.get(i);
14             if(a>t){
15                 lst.set(i, a);  // 填空
16                 a = t;  
17             }
18         }
19     
20         return b;
21     }
22 
23     public static void main(String[] args)
24     {
25         List<Integer> lst = new Vector<Integer>();
26         lst.addAll(Arrays.asList(12,127,85,66,27,34,15,344,156,344,29,47));
27         System.out.println(max5(lst));
28         
29     }
30     
31 }

第八题

看不懂,懒得做

第九题

用暴力法解决如下:

 1 public class t9 {
 2     
 3     public static void main(String[] args) {
 4         
 5         char[] a = {' ', '+', '-'};
 6         char[] num = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
 7         for(char x1: a)
 8             for(char x2: a)
 9                 for(char x3: a)
10                     for(char x4: a)
11                         for(char x5: a)
12                             for(char x6: a)
13                                 for(char x7: a)
14                                     for(char x8: a) {
15                                         int res = 0;
16                                         char[] s = {x1, x2, x3, x4, x5, x6, x7, x8};
17                                         String str = "";
18                                         for(int i=0; i<=7; i++) {
19                                             str = str + num[i] + s[i]; 
20                                         }
21                                         str = str + num[8];
22                                         str = str.replaceAll(" ", "");
23                                         String[] subStrs = str.split("-");
24                                         int[] addResults = new int[subStrs.length];
25                                         
26                                         // 把第一个减号之前的都加起来
27                                         String startStr = subStrs[0];
28                                         String[] startStrs = startStr.split("[+]");
29                                         for(int i=0; i<startStrs.length; i++) {
30                                             res += Integer.parseInt(startStrs[i]);
31                                         }
32                                         
33                                         // 第一个减号之后的
34                                         for(int i=1; i<subStrs.length; i++) {
35                                             String[] strs = subStrs[i].split("[+]");
36                                             // -
37                                             res -= Integer.parseInt(strs[0]);
38                                             for(int j=1; j<strs.length; j++) {
39                                                 // +
40                                                 res += Integer.parseInt(strs[j]);
41                                             }
42                                         }
43                                         if(res==110) {
44                                             System.out.println(str);
45                                         }
46                                     }
47         
48     }
49     
50 }

第十题

原文地址:https://www.cnblogs.com/wyb666/p/10731922.html