蓝桥日记⑤2014第五届省赛(软件类)JavaA组❆答案解析
Posted 白鳯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥日记⑤2014第五届省赛(软件类)JavaA组❆答案解析相关的知识,希望对你有一定的参考价值。
【蓝桥日记⑤】2014第五届省赛(软件类)JavaA组☃答案解析
文章目录
1、猜年龄
解法:暴力枚举
package fiveSession;
/*** 2014第五届 1、猜年龄 ***/
public class test1
public static void main(String[] args)
int age1 = 0, age2 = 0;
boolean find = false;
for (int i = 1; i < 50; i++)
for (int j = i + 1; j < i + 9; j++)
if (i * j == 6 * (i + j))
age1 = i;
age2 = j;
find = true;
break;
if (find) break;
System.out.println(age1);
答案:
10
2、李白打酒
解法:递归
package fiveSession;
/*** 2014第五届 2、李白打酒 ***/
public class test2
public static void main(String[] args)
int wine = 2;
int shop = 5, flower = 10;
int res = f(wine, shop, flower);
System.out.println(res);
private static int f(int wine, int shop, int flower)
if (shop < 0 || flower < 0) return 0;
if (wine == 0 && (shop != 0 || flower != 0)) return 0;
if (wine == 0 && shop == 0 && flower == 0) return 1;
int res = f(wine * 2, shop - 1, flower);
res += f(wine - 1, shop, flower - 1);
return res;
递归二
package fiveSession;
/*** 2014第五届 2、李白打酒 ***/
public class test2
static int res = 0;
public static void main(String[] args)
int wine = 2;
int shop = 5, flower = 10 - 1;
f(wine, shop, flower);
System.out.println(res);
private static void f(int wine, int shop, int flower)
if (wine == 1 && shop == 0 && flower == 0)
res++;
return;
if (shop > 0) f(wine * 2, shop - 1, flower);
if (flower > 0) f(wine - 1, shop, flower - 1);
答案:
14
3、神奇算式
解法:暴力枚举
package fiveSession;
import java.util.Arrays;
/*** 2014第五届 3、神奇算式 ***/
public class test3
public static void main(String[] args)
int res = 0;
for (int i = 1; i < 10; i++)
for (int j = 0; j < 10; j++)
if (i == j) continue;
for (int k = 0; k < 10; k++)
if (i == k || j == k) continue;
for (int p = 0; p < 10; p++)
if (i == p || j == p || k == p) continue;
int src = i * 1000 + j * 100 + k * 10 + p;
if (j != 0)
res += check(src, i * (j * 100 + k * 10 + p));
if (k != 0 && (i * 10 + j) < (k * 10 + p))
res += check(src, (i * 10 + j) * (k * 10 + p));
System.out.println(res);
public static int check(int src, int r2)
String s1 = String.valueOf(src);
String s2 = String.valueOf(r2);
char[] cs1 = s1.toCharArray();
char[] cs2 = s2.toCharArray();
Arrays.sort(cs1);
Arrays.sort(cs2);
if (new String(cs1).equals(new String(cs2))) return 1;
return 0;
答案:
12
4、写日志
解法:找规律
package fiveSession;
/*** 2014第五届 4、写日志 ***/
public class test4
private static int n = 1;
public static void write(String msg)
String filename = "t" + n + ".log";
n = (n % 3) + 1;
System.out.println(filename + "," + msg);
public static void main(String[] args)
for (int i = 0; i < 15; i++) write("a");
答案:
(n % 3) + 1
5、锦标赛
package fiveSession;
/*** 2014第五届 5、锦标赛 ***/
public class test5
static void f(int[] a)
int n = 1;
while (n < a.length) n *= 2;
int[] b = new int[2 * n - 1];
for (int i = 0; i < n; i++)
if (i < a.length)
b[n - 1 + i] = i;
else
b[n - 1 + i] = -1;
//从最后一个向前处理
for (int i = b.length - 1; i > 0; i -= 2)
if (b[i] < 0)
if (b[i - 1] >= 0)
b[(i - 1) / 2] = b[i - 1];
else
b[(i - 1) / 2] = -1;
else
if (a[b[i]] > a[b[i - 1]])
b[(i - 1) / 2] = b[i];
else
b[(i - 1) / 2] = b[i - 1];
//输出树根
System.out.println(b[0] + ":" + a[b[0]]);
//值等于根元素的需要重新pk
pk(a, b, 0, b[0]);
//再次输出树根
System.out.println(b[0] + ":" + a[b[0]]);
static void pk(int[] a, int[] b, int k, int v)
//if(k>b.length) return;
int k1 = k * 2 + 1;
int k2 = k1 + 1;
if (k1 >= b.length || k2 >= b.length)
// 此处将 叶子结点为最大值的 下标b[k]抹去,置为-1
b[k] = -1; //(要是我我会该行代码设为考点~(@^_^@)~)
return;
if (b[k1] == v)
pk(a, b, k1, v);
else
pk(a, b, k2, v);
//重新比较
if (b[k1] < 0)
if (b[k2] >= 0)
b[k] = b[k2];
else
b[k] = -1;
return;
if (b[k2] < 0)
if (b[k1] >= 0)
b[k] = b[k1];
else
b[k] = -1;
return;
if (a[b[k1]]>a[b[k2]])
b[k] = b[k1];
else
b[k] = b[k2];
public static void main(String[] args)
int[] a = 54, 55, 18, 16, 122, 255, 30, 9, 58, 66;
f(a);
答案:
a[b[k1]]>a[b[k2]]
6、六角填数
解法:全排列+剪枝
package fiveSession;
/*** 2014第五届 6、六角填数 ***/
public class test6
static int[] a = 2, 4, 5, 6, 7, 9, 10, 11, 12;
public static void main(String[] args)
backtrack(0, a.length);
private static void backtrack(int begin, int end)
if (begin == 6 && 8 + a[0] + a[1] + a[2] != 1 + a[0] + a[3] + a[5]) return;
if (begin == 7 && 8 + a[0] + a[1] + a[2] != 8 + a[3] + a[6] + 3) return;
if (begin == end)
if (8 + a[0] + a[1] + a[2] != a[5] + a[6] + a[7] + a[8]) return;
if (8 + a[0] + a[1] + a[2] != a[2] + a[4] + a[7] + 3) return;
if (8 + a[0] + a[1] + a[2] != 1 + a[1] + a[4] + a[8]) return;
System.out.println(a[3]);
for (int i = begin; i < end; i++)
int t = a[begin]; a[begin] = a[i]; a[i] = t;
backtrack(begin + 1, end);
t = a[begin]; a[begin] = a[i]; a[i] = t;
答案:
10
7、绳圈
解法:动态规划
绳圈组合数:自成一圈+加入前一组合(2个头+i -1个分割点)
令C[i]
表示i个绳的组合数,则有
C
[
i
]
=
c
[
i
−
1
]
+
C
[
i
−
1
]
∗
(
i
−
1
)
∗
2
=
C
[
i
−
1
]
∗
(
2
i
−
1
)
C[i] = c[i -1] + C[i - 1] *(i - 1) * 2 = C[i-1]*(2i-1)
C[i]=c[i−1]+C[i−1]∗(i−1)以上是关于蓝桥日记⑤2014第五届省赛(软件类)JavaA组❆答案解析的主要内容,如果未能解决你的问题,请参考以下文章
蓝桥日记①2017第八届省赛(软件类)JavaA组❤️答案解析
蓝桥日记③2016第七届省赛(软件类)JavaA组✿答案解析
蓝桥日记④2015第六届省赛(软件类)JavaA组➤答案解析
蓝桥日记⑥2013第四届省赛(软件类)JavaA组@答案解析