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

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

题目及解析如下:

题目大致介绍:

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

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

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

第一题

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

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

 // 黄金分割数

 public class t1 {

     public static void main(String[] args) {

         int a = 1;
int b = 3; for(int i=0; i<=50; i++) {
int temp = a + b;
double s = (double)b/temp;
System.out.println(b + " " + temp + " " + s);
a = b;
b = temp;
} } // answer: 5778 9349 0.6180340143330838
// 最后提交的答案: 5778 9349
// 注: 为了求稳 算出答案后可以用电脑中带的计算器验算一下 }

第二题

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

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

 // 海盗比酒量

 public class t2 {

     public static void main(String[] args) {

         for (int m = 1; m <= 20; m++) {
for (int n = 1; n < m; n++) {
for (int p = 1; p < n; p++) {
for (int q = 1; q < p; q++) {
if (n * p * q + m * p * q + m * n * q + m * n * p == m * n * p * q) {
System.out.println(m + "," + n + "," + p + "," + q + "," + 0);
}
}
}
}
} } }

第三题

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

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

写代码计算如下:

 import java.math.BigInteger;

 public class t3 {

     public static void f(int n) {
BigInteger a = new BigInteger("1");
for (int i = 1; i <= n; i++) {
a = a.multiply(new BigInteger("2"));
} a = a.subtract(new BigInteger("1"));
System.out.println(a.toString());
} public static void f2(int n) {
//
long a = 1;
for (int i = 1; i <= n; i++) {
a = a * 2;
} System.out.println(a - 1);
} public static void main(String[] args) { f(1); // 1
f(2); //
f(3); // f(64); // f2(63); // 将这个答案*2+1就是最后的答案(也就是f(64))
f2(64); // 爆了、、、
} }

第四题

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

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

 public class t4 {

     public static void f(int score, int n, String str) {
if (n == 10 && score == 100) {
System.out.println(str);
return;
}
if (n == 10) {
return;
} f(score * 2, n + 1, str + "1"); // 答对
f(score - (n + 1), n + 1, str + "0"); // 答错 } public static void main(String[] args) { f(10, 0, ""); // answer:
// 1011010000
// 0111010000
// } }

第五题

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

常规题,代码如下:

 public class t5 {

     public static int getFirstNum(String s) {
if(s == null || s.length() == 0) {
return -1;
}
char c = s.charAt(0);
if(c>='0' && c<='9') {
return c - '0';
}
return getFirstNum(s.substring(1));
} public static void main(String[] args) { System.out.println(getFirstNum("abc24us43"));
System.out.println(getFirstNum("82445adb5"));
System.out.println(getFirstNum("ab")); } }

第六题

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

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

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

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

 public class t6 {

     public static void main(String[] args)
{
System.out.println("标准 " + Math.PI); double a = 1;
int n = 6; for(int i=0; i<10; i++){ // 可以把n改成100 看更多的结果
double b = Math.sqrt(1-(a/2)*(a/2));
a = Math.sqrt((1-b)*(1-b) + (a/2)*(a/2)); n = 2 * n; //填空 System.out.println(n + "  " + a*n/2); // 填空
}
} }

第七题

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

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

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

 import java.util.*;

 public class t7 {

     public static List<Integer> max5(List<Integer> lst)
{
if(lst.size()<=5) return lst; int a = lst.remove(0); // 填空
List<Integer> b = max5(lst); for(int i=0; i<b.size(); i++){
int t = b.get(i);
if(a>t){
lst.set(i, a); // 填空
a = t;
}
} return b;
} public static void main(String[] args)
{
List<Integer> lst = new Vector<Integer>();
lst.addAll(Arrays.asList(12,127,85,66,27,34,15,344,156,344,29,47));
System.out.println(max5(lst)); } }

第八题

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

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

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

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

看不懂,懒得做

第九题

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

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

用暴力法解决如下:

 public class t9 {

     public static void main(String[] args) {

         char[] a = {' ', '+', '-'};
char[] num = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
for(char x1: a)
for(char x2: a)
for(char x3: a)
for(char x4: a)
for(char x5: a)
for(char x6: a)
for(char x7: a)
for(char x8: a) {
int res = 0;
char[] s = {x1, x2, x3, x4, x5, x6, x7, x8};
String str = "";
for(int i=0; i<=7; i++) {
str = str + num[i] + s[i];
}
str = str + num[8];
str = str.replaceAll(" ", "");
String[] subStrs = str.split("-");
int[] addResults = new int[subStrs.length]; // 把第一个减号之前的都加起来
String startStr = subStrs[0];
String[] startStrs = startStr.split("[+]");
for(int i=0; i<startStrs.length; i++) {
res += Integer.parseInt(startStrs[i]);
} // 第一个减号之后的
for(int i=1; i<subStrs.length; i++) {
String[] strs = subStrs[i].split("[+]");
// -
res -= Integer.parseInt(strs[0]);
for(int j=1; j<strs.length; j++) {
// +
res += Integer.parseInt(strs[j]);
}
}
if(res==110) {
System.out.println(str);
}
} } }

第十题

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

上一篇:OpenWrt路由器通过LuCI界面实现Guest SSID功能


下一篇:Logstash 性能及其替代方案