# 2015年第6届蓝桥杯Java B组省赛试题解析

iceq

### 1、三角形面积

``````计算方法：
8 * 8 - （8 * 2 / 2 + 6 * 4 / 2 + 8 * 4 / 2）= 64 - (8+ 12 + 16) =64-36=28``````

### 2、立方变自身

1^3 = 1
8^3 = 512 5+1+2=8
17^3 = 4913 4+9+1+3=17
...

``````public class CubeEqual {
public static void main(String[] args) {

int count = 0;
for (int i = 1; i < 100; i++) {
int s = i * i * i;
if (i == fun(s)) {
System.out.println("i = " + i);
System.out.println("s = " + s);
System.out.println("fun(s) = " + fun(s));
count++;
}
}
System.out.println("count = " + count);
}

static int fun(int a) {
if (a < 10) {
return a;
}
return fun(a / 10) + a % 10;
}
}

### 3、三羊献瑞

``````容易推测得到：

### 4、循环节长度

``````public static int f(int n, int m)
{
n = n % m;
Vector v = new Vector();

for(;;)
{
n *= 10;
n = n % m;
if(n==0) return 0;
if(v.indexOf(n)>=0)  _________________________________ ;  //填空
}
}
``````

``答案: return v.size()-v.index(n)``

### 5、 九数组分数

1,2,3…9 这九个数字组成一个分数，其值恰好为1/3，如何组法？

``````public class A
{
public static void test(int[] x)
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) System.out.println(a + " " + b);
}

public static void f(int[] x, int k)
{
if(k>=x.length){
test(x);
return;
}

for(int i=k; i<x.length; i++){
{int t=x[k]; x[k]=x[i]; x[i]=t;}
f(x,k+1);
_______________________________________       // 填空
}
}

public static void main(String[] args)
{
int[] x = {1,2,3,4,5,6,7,8,9};
f(x,0);
}
}``````

``````/**
* 该类主要用户打印排列
*
* @author yunqing_shui@163.com
*/
public class Main {
public static int count = 0;

public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
per(a, 0, 4);
System.out.println(count);
}

public static void swapTwoNumber(int a[], int i, int j) {
if (i != j) {
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
}

/**
* @param a
* @param index 数组的起始位置
* @param len   要排列的长度
*/
public static void per(int a[], int index, int len) {
if (index == len) {
print(a, len);
}
for (int i = index; i < len; i++) {
// 交换两个数
swapTwoNumber(a, index, i);
per(a, index + 1, len);
// 交换两个数
swapTwoNumber(a, index, i);
}
}

public static void print(int a[], int len) {
for (int i = 0; i < len; i++) {
System.out.print(a[i]);
}
count++;
System.out.println();
}
}``````

``````   for (int i = index; i < len; i++) {
// 交换两个数
swapTwoNumber(a, index, i);
per(a, index + 1, len);
// 交换两个数
swapTwoNumber(a, index, i);
}``````
``````  for(int i=k; i<x.length; i++){
{int t=x[k]; x[k]=x[i]; x[i]=t;}
f(x,k+1);
_______________________________________       // 填空
}``````

``答案：{int t=x[k]; x[k]=x[i]; x[i]=t;}``

### 6、加法变乘法

`1+2+3+…+10 * 11+12+…+27*28+29+…+49 = 2015`

``````/**
*
* @author yunqing_shui@163.com
*/
public class Main6 {

public static void main(String[] args) {
int a[] = new int[49];
for (int i = 0; i < 49; i++) {
a[i] = i+1;
}

int b[] = new int[47];

for (int x = 0; x < 46; x++) {
for (int y = x + 2; y <= 48; y++) {

for (int i = 0, j = 0; i < 47 && j < 49; i++, j++) {
if (i == x || i == y) {
int s = a[j] * a[j + 1];
b[i] = s;
j++;
} else {
b[i] = a[j];
}
}

if (sum(b) == 2015) {
System.out.println("x = " + (x+1));
System.out.println("y = " + (y+2));
System.out.println("Arrays.toString(a) = " + Arrays.toString(a));
System.out.println("Arrays.toString(b) = " + Arrays.toString(b));
}
}
}

System.out.println("sum = " + sum(b));
}

public static int sum(int a[]) {
int s = 0;
for (int i = 0; i < a.length; i++) {
s += a[i];
}
return s;
}
}
``````
``答案：16``

### 7、牌型种数

``````public class Seven3 {

public static int ans = 0;

static void dfs(int type, int sum) {
if (sum > 13) return;
if (type == 13) {
if (sum == 13) ans++;
return;
}
for (int i = 0; i < 5; i++) {
dfs(type + 1, sum + i);
}
}

public static void main(String[] args) {
dfs(0, 0);
System.out.println("ans = " + ans);
}
}``````

#### 2、线性规划的解法

``````public class Seven {

public static void main(String[] args) {
int[][] dp = new int[14][14];
dp[0][0] = 1;
for (int i = 1; i < 14; i++)
for (int j = 0; j < 14; j++)
for (int k = 0; k < 5; k++)
if (j + k <= 13)
dp[i][j + k] += dp[i - 1][j];
System.out.println(dp[13][13]);
}
}``````

``````public class Seven2 {

public static void main(String[] args) {
int dp[][] = new int[14][14];
dp[1][0] = dp[1][1] = dp[1][2] = dp[1][3] = dp[1][4] = 1;

for (int i = 2; i <= 13; i++) {
for (int j = 0; j <= 13; j++) {
if (j - 4 >= 0) dp[i][j] += dp[i-1][j-4];
if (j - 3 >= 0) dp[i][j] += dp[i-1][j-3];
if (j - 2 >= 0) dp[i][j] += dp[i-1][j-2];
if (j - 1 >= 0) dp[i][j] += dp[i-1][j-1];
dp[i][j] += dp[i-1][j];
}
}
System.out.println(dp[13][13]);
}
}``````

1、这种牌取0个
2、这种牌取1个
3、这种牌取2个
4、这种牌取3个
5、这种牌取4个

``````  dp[1][0] = dp[1][1] = dp[1][2] = dp[1][3] = dp[1][4] = 1;

dp[2][0] = dp[1][0] =1 ;``````

``dp[2][1] = dp[1][0] +dp[1][1] ;``

``dp[2][2] = dp[1][0] +dp[1][1]+dp[1][2] ;``

``dp[2][3] = dp[1][0] +dp[1][1]+dp[1][2]+d[1][3] ;``

``````dp[2][4] = dp[1][0] + dp[1][1] + dp[1][2] + dp[1][3] + d[1][4] ;
dp[2][5] = dp[1][1] + dp[1][2] + dp[1][3] + dp[1][4] + d[1][5] ;``````

``````public class Seven2 {

public static void main(String[] args) {
int dp[][] = new int[14][14];
dp[1][0] = dp[1][1] = dp[1][2] = dp[1][3] = dp[1][4] = 1;

for (int i = 2; i <= 13; i++) {
for (int j = 0; j <= 13; j++) {
if (j - 4 >= 0) dp[i][j] += dp[i-1][j-4];
if (j - 3 >= 0) dp[i][j] += dp[i-1][j-3];
if (j - 2 >= 0) dp[i][j] += dp[i-1][j-2];
if (j - 1 >= 0) dp[i][j] += dp[i-1][j-1];
dp[i][j] += dp[i-1][j];
}
}
System.out.println(dp[13][13]);
}
}``````

#### 第三种解法

``````import java.util.ArrayList;
import java.util.List;

/**
* @author yunqing_shui@163.com
*/
public class Main7 {
public static int S = 0;

public static void main(String[] args) {

ArrayList<Integer> list0 = new ArrayList<>();
for (int i = 0; i < 4; i++) {
ArrayList<Integer> list1 = new ArrayList<>(list0);
for (int j = 0; j < 5; j++) {
ArrayList<Integer> list2 = new ArrayList<>(list1);
for (int k = 0; k < 7; k++) {
ArrayList<Integer> list3 = new ArrayList<>(list2);
for (int l = 0; l < 14; l++) {
sumList(list3);
}
}
}
}
System.out.println("S = " + S);

}

public static void sumList(List<Integer> list) {
int sum = 0;
for (int i : list) {
sum += i;
}
int a4 = 0;
int a3 = 0;
int a2 = 0;
int a1 = 0;
if (sum == 13) {
for (int it : list) {
if (it == 1) {
a1++;
} else if (it == 2) {
a2++;
} else if (it == 3) {
a3++;
} else if (it == 4) {
a4++;
}
}
int number = getCNM(13, a1) * getCNM(13 - a1, a2) * getCNM(13 - a1 - a2, a3) * getCNM(13 - a1 - a2 - a3, a4);
S += number;
}

}

public static int getANM(int a, int b) {
int s = 1;
for (int i = a; i > Math.max(b, a - b); i--) {
s *= i;
}
return s;
}

public static int getCNM(int a, int b) {
return getANM(a, b) / getN(Math.min(b, a - b));
}

public static int getN(int a) {
if (a == 1 || a == 0) {
return 1;
}
return a * getN(a - 1);
}
}
``````

211 声望
19 粉丝
0 条评论