Analysis and Summary of the 2015 6th Blue Bridge Cup Java Group B Provincial Competition


  • Note: Part of the code and procedures are derived from the official website video of Lanqiao Cup (analysis of real questions over the years),  Teacher Zheng Wei .
  1. 2013 04th Blue Bridge Cup Java Group B provincial competition detailed explanation and summary
  2. Detailed explanation and summary of the real questions of the 05th Blue Bridge Cup Java Group B Provincial Competition in 2014
  3. Detailed explanation and summary of the real questions of the 2015 Blue Bridge Cup Java Group B Provincial Competition
  4. 2016 07th Blue Bridge Cup Java Group B provincial competition detailed explanation and summary
  5. 2017 08th Blue Bridge Cup Java Group B provincial competition detailed explanation and summary
  6. Detailed explanation and summary of the real questions of the 09th Blue Bridge Cup Java Group B Provincial Competition in 2018
  7. 2019 10th Blue Bridge Cup Java Group B provincial competition detailed explanation and summary
  8. 2020 The 11th Blue Bridge Cup Java Group B 1st simulation game detailed explanation and summary (in-school simulation)
  9. 2020 The 11th Blue Bridge Cup Java Group B 2nd simulation game detailed explanation and summary
  10. 2020 The 11th Blue Bridge Cup C/C++ Group B provincial competition detailed explanation and summary [The 1st provincial competition 2020.7.5] [Java Edition]
  11. 2020 The 11th Blue Bridge Cup Java Group B provincial competition detailed explanation and summary [The 1st provincial competition 2020.7.5]
  12. 2020 The 11th Blue Bridge Cup Java Group C provincial competition detailed explanation and summary [The first provincial competition 2020.7.5]

table of Contents

1. Triangle area

Second, the cube becomes itself

Three, three sheep offering Rui

Fourth, the length of the loop section

Five and nine group scores

Six, addition to multiplication

Seven, the number of card types

8. Drink redemption

Nine, base dice

10. Tree of Life

summary


    

 

1. Triangle area

Triangle area

As shown in Figure 1. The area of ​​all small squares in the figure is 1.

Map 1.jpg


So, what should be the area of ​​the triangle in the figure?

Please fill in the area of ​​the triangle. Do not fill in any redundant content or explanatory text.

[Answer]: 28

[Analysis]: 64-36

Second, the cube becomes itself

Cube itself

Observe the following phenomenon, the cube of a certain number, bitwise accumulation is still equal to itself.
1^3 = 1 
8^3 = 512 5+1+2=8
17^3 = 4913 4+9+1+3=17
...

Could you please count the number of positive integers that meet this property, including 1, 8, 17?

Please fill in the number, do not fill in any redundant content or explanatory text.

[Answer]: 6

package provincialGames_06_2015;

public class A02_立方变自身 {
	private static int ans;

	public static void main(String[] args) { // 6
		for (int i = 1; i < 99; i++) {
			int i1 = i * i * i;
			int sum = sum(i1);
			if (sum == i) {
				System.out.println(i + " " + i1);
				ans++;
			}
		}
		System.out.println(ans);
	}

	private static int sum(int x) {
		String s = String.valueOf(x);
		int sum = 0;
		for (int i = 0; i < s.length(); i++) {
			sum += s.charAt(i) - '0';
		}
		return sum;
	}
}

Three, three sheep offering Rui

Sanyang Xianrui

Observe the following addition formula:

      Auspicious brilliance
  + Three sheeps offering Rui
-------------------
   Three sheeps radiating aura

(If there is an alignment problem, please refer to [Figure 1.jpg])

Map 1.jpg

Among them, the same Chinese characters represent the same numbers, and different Chinese characters represent different numbers.

Please fill in the 4 digits represented by "Sanyang Xianrui" (the answer is only), do not fill in any extra content.

[Answer]: 1085

a b c d
+   e f g b
-------------------
e f c b i

e=1,a=9,f=0,c=b+1,c+g>10

package provincialGames_06_2015;

public class A03_三羊献瑞 {

	public static void main(String[] args) {
		for (int b = 2; b < 9; ++b) {
			for (int d = 2; d < 9; ++d) {
				if (b == d)
					continue;
				for (int g = 2; g < 9; ++g) {
					if (g == b || g == d)
						continue;
					int c = b + 1;
					if (c == b || c == d || c == g)
						continue;
					if (c + g <= 10)
						continue;
					int sum = 9000 + b * 100 + c * 10 + d + 1000 + g * 10 + b;
					for (int i = 2; i < 9; ++i) {
						if (i == b || i == d || i == g || i == c)
							continue;
						if (sum <= (10000 + c * 100 + b * 10 + i) && sum >= (10000 + c * 100 + b * 10 + i)) {
							System.out.printf("%2d%d%d%d\n", 9, b, c, d);
							System.out.printf("%2d%d%d%d\n", 1, 0, g, b);
							System.out.printf("%d\n", sum);
							System.out.printf("---------\n");
						}
					}
				}
			}
		}
	}
}

Fourth, the length of the loop section

Loop length

Dividing two integers will sometimes produce cyclic decimals. The cyclic part is called: cyclic section.
For example, 11/13=6=>0.846153846153..... Its loop section is [846153], which has 6 digits.
The following method can find the length of the loop section.

Please read the code carefully and fill in the missing code in the underlined part.

    public static int f(int n, int m)
    {
        n = n % m;    
        Vector v = new Vector();
        
        for(;;)
        {
            v.add(n);
            n *= 10;
            n = n % m;
            if(n==0) return 0;
            if(v.indexOf(n)>=0)  _________________________________ ;  //填空
        }
    }

Note that you can only fill in the missing parts and do not copy existing codes repeatedly. Do not fill in any extra text.

[Answer]: return v.size()-v.indexOf(n)

package provincialGames_06_2015;

import java.util.Vector;

public class A04_循环节长度 {
	public static int f(int n, int m) {
		n = n % m;
		Vector v = new Vector();

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

	public static void main(String[] args) {
		System.out.println(f(11, 13));
		System.out.println(f(7, 18));
	}
}

Five and nine group scores

Nine group scores

1,2,3...9 These nine numbers form a score, and its value is exactly 1/3. How to group it?

The following program implements this function, please fill in the missing code in the underlined part.

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);
    }
}

Note that you can only fill in the missing parts and do not copy existing codes repeatedly. Do not fill in any extra text.

[Answer]: {int t=x[k]; x[k]=x[i]; x[i]=t;}

package provincialGames_06_2015;

public class A05_九数组分数 {
	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);
			{
				int t = x[k];
				x[k] = x[i];
				x[i] = t;
			} // 填空
		}
	}

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

Six, addition to multiplication

Addition to multiplication

We all know: 1+2+3+ ... + 49 = 1225
Now you are required to turn the two non-adjacent plus signs into multiplication signs, making the result 2015

For example:
1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
is the answer that meets the requirements.

Please look for another possible answer and submit the number to the left of the multiplication sign in the front position (for example, submit 10).

Note: What you need to submit is an integer, do not fill in any extra content.

[Answer]: 16

package provincialGames_06_2015;

public class A06_加法变乘法 {

	public static void main(String[] args) {
		for (int i = 1; i <= 46; i++) {
			for (int j = i + 2; j <= 48; j++) {
				if (i * (i + 1) - (i + i + 1) + j * (j + 1) - (j + j + 1) == 2015 - 1225)
					System.out.println(i + " " + j);
			}
		}
	}

}

Seven, the number of card types

Number of card types

Xiao Ming was hijacked to X Casino and forced to play cards with three other people.
A deck of playing cards (without the big and small trump cards, a total of 52), evenly distributed to 4 people, each with 13 cards.
At this time, a question suddenly popped up in Xiao Ming's mind:
If you don't consider suits, only points, and don't consider the order of the cards you get, how many kinds of initial card combinations can you get in your hand?

Please fill in the whole number, do not fill in any extra content or explanatory text.

[Answer]: 3598180

package provincialGames_06_2015;

public class A07_牌型种数 {

	private static int ans;

	public static void main(String[] args) {
		f(0, 0);
		System.out.println(ans);
	}
	//13堆牌,每堆4个, 每堆可选0到4个, 只要牌的总数为13即可
	//k: 哪种牌;   cnt: 总共分配了几张牌
	private static void f(int k, int cnt) {
		if (k > 13 || cnt > 13)
			return;
		if (k == 13 && cnt == 13) {
			ans++;
			return;
		}
		//cnt 每一种牌出现的次数,cnt逐步上升
		//cnt==13,牌型种数+1,返回
		for (int i = 0; i < 5; i++) {
			f(k + 1, cnt + i);
		}
	}

}

8. Drink redemption

Drink redemption

Leyangyang Beverage Factory is holding a promotional activity. For Leyangyang Type C Beverage, you can exchange for another bottle of Type C Beverage with 3 caps, and it can be recycled forever, but credit is not allowed.

Please calculate, if Xiao Ming does not waste bottle caps and participates in activities as much as possible, then for the n bottles of beverages he initially bought, how many bottles of beverages he can get in the end.

Input: an integer n, indicating the number of drinks to start buying (0<n<10000)
Output: an integer, indicating the number of drinks actually obtained

For example:
User input:
100 The
program should output:
149

User input:
101 The
program should output:
151


Resource agreement:
Peak memory consumption (including virtual machines) <256M
CPU consumption <1000ms


Please output strictly according to the requirements, and don't superfluously print the extra content like: "please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.
Note: Do not use the package statement. Do not use the features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.

package provincialGames_06_2015;

import java.util.Scanner;

public class A08_饮料换购 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		while (n >= 3) {
			n -= 2;
			ans += 3;
		}
		ans += n;
		System.out.println(ans);
	}

}

Nine, base dice

Base dice

The gambler atm became obsessed with dice in his later years, that is, put the dice on top of the other, not crookedly, but to form a square cylinder.
After long-term observation, atm has discovered the mystery of stable dice: the faces of some numbers will repel each other!
Let's first standardize the dice: the opposite of 1 is 4, the opposite of 2 is 5, and the opposite of 3 is 6.
Assuming that there are m groups of mutual exclusion, the faces of the two numbers in each group are close to each other, and the dice cannot be stably built up. Atm wants to calculate how many different possible ways to base the dice.
The two ways to base the dice are the same, if and only if the directions of the corresponding numbers of the dice of the corresponding height in the two ways are the same.
Since there may be too many solutions, please output the result modulo 10^9 + 7.

Don’t underestimate the number of dice in ATM~

The
two integers nm
n in the first line of "input format" represent the number of dice. The
next m lines contain two integers ab in each line, which means that a and b cannot be close together.

"Output Format" has
a number on a line, which means the answer modulo 10^9 + 7.

"Sample Input"
2 1
1 2

"Sample Output"
544

"Data Range"
For 30% data: n <= 5
For 60% data: n <= 100
For 100% data: 0 <n <= 10^9, m <= 36


Resource agreement:
peak memory consumption (including virtual machines) <256M
CPU consumption <2000ms


Please output strictly according to the requirements, and don't superfluously print the extra content like: "please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.
Note: Do not use the package statement. Do not use the features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.

package provincialGames_06_2015;

import java.util.Scanner;

public class A09_垒骰子 {
	static int op[] = new int[7];
	private static int n;
	private static int m;
	private static final long MOD = 1000000007;

	static void init() {
		op[1] = 4;
		op[4] = 1;
		op[2] = 5;
		op[5] = 2;
		op[3] = 6;
		op[6] = 3;
	}

	public static void main(String[] args) {
		init();
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		long conflict[][] = new long[6][6];
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 6; j++) {
				conflict[i][j] = 1;
			}
		}
		// 建立冲突矩阵
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			conflict[op[a] - 1][b - 1] = 0;
			conflict[op[b] - 1][a - 1] = 0;
		}
		// 求冲突矩阵的n-1次方
		long[][] mPow_n_1 = mPow(conflict, n - 1);
		// 累加矩阵的每个元素
		long ans = 0;
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 6; j++) {
				ans = (ans + mPow_n_1[i][j]) % MOD;
			}
		}
		// ans*4^n
		System.out.println(ans * power(4, n) % MOD);
	}
	
	//快速求 i^n
	private static long power(long i, int n) {
		long ans = 1;
		while (n != 0) {
			if ((n & 1) == 1)
				ans = (ans * i) % MOD;
			i = i * i % MOD;
			n >>= 1;
		}
		return ans;
	}

	/* 矩阵的快速幂 */
	private static long[][] mPow(long[][] conflict, int n) {
		long[][] e = new long[6][6];
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 6; j++) {
				if (i == j)
					e[i][j] = 1;
				else
					e[i][j] = 0;
			}
		}
		while (n != 0) {
			if ((n & 1) == 1) {
				e = mMul(e, conflict);
			}
			conflict = mMul(conflict, conflict);
			n >>= 1;
		}

		return e;
	}

	private static long[][] mMul(long[][] a, long[][] b) {
		long[][] ans = new long[6][6];
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 6; j++) {
				for (int k = 0; k < 6; k++) {
					ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % MOD;
				}
			}
		}
		return ans;
	}
}

10. Tree of Life

tree of Life

In the X forest, God created the tree of life.

He marked an integer on each node (leaf is also called a node) of each tree, representing the harmony value of this point.
God wants to select a non-empty node set S in this tree, so that for any two points a, b in S, there is a point sequence {a, v1, v2, ..., vk, b} such that Each point in this point sequence is an element in S, and there is an edge connected between two adjacent points in the sequence.

Under this premise, God wants to make the sum of the integers corresponding to the points in S as large as possible.
The biggest sum is the score given by God to the tree of life.

After atm's efforts, he already knew the integer that God gave to each node on each tree. But because ATM is not good at calculation, he doesn't know how to effectively score. He needs you to write a program for him to calculate the score of a tree.


An integer n in the first line of "input format" means that the tree has n nodes.
The n integers in the second row indicate the score of each node in turn.
In the next n-1 lines, each line has 2 integers u, v, indicating that there is an edge from u to v. Since this is a tree, there is no ring.

"Output Format"
outputs a number on a line, which represents the score given by God to this tree.

"Sample Input"
5
1 -2 -3 4 5
4 2
3 1
1 2
2 5

"Sample Output"
8

"Data Range"
For 30% data, n <= 10
For 100% data, 0 <n <= 10^5, the absolute value of the score of each node does not exceed 10^6.

Resource agreement:
peak memory consumption (including virtual machines) <256M
CPU consumption <3000ms


Please output strictly according to the requirements, and don't superfluously print the extra content like: "please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.
Note: Do not use the package statement. Do not use the features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.

package provincialGames_06_2015;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class A10_生命之树 {

	private static int n;
	private static long[] w;
	private static List<Integer>[] g;
	private static long ans;
	// List<Integer>[] x = new ArrayList[n+1];

	public static void main(String[] args) throws FileNotFoundException {
		System.setIn(new FileInputStream(new File("F:/Java/eclipse-jee-2019-09-R-win32-x86_64/eclipse-workspace/zLanQiao/src/provincialGames_06_2015/data10/in4.txt")));
		// in4.txt 正常运行;in5.txt 栈溢出
		// /Users/zhengwei/workspace/lanqiaobei2019/src/2015_Java_B/data10/in5.txt
		// F:\Java\eclipse-jee-2019-09-R-win32-x86_64\eclipse-workspace\zLanQiao\src\provincialGames_06_2015\data10\in4.txt
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		w = new long[n + 1];
		g = new ArrayList[n + 1];
		initG();
		for (int i = 1; i <= n; i++) {
			w[i] = sc.nextLong();
		}
		for (int i = 0; i < n - 1; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			g[a].add(b); // 无向的邻接表 双向
			g[b].add(a); // 有向图 例外
		}
		dfs(1, 0);
		System.out.println(ans);
	}

	// u作为根所代表的子树有一个最大权和,将其存储在w[u]中
	private static void dfs(int u, int fa) {
		for (int i = 0; i < g[u].size(); i++) {
			Integer child = g[u].get(i);
			if (child == fa)
				continue;
			dfs(child, u);
			if (w[child] > 0)
				w[u] += w[child];
		}
		if (w[u] > ans)
			ans = w[u];
	}

	// 邻接表 在Java中,一般用List来做, 数组List<Integer>[] g = new ArrayList[n+1];
	// 对象数组,只对 对象 进行了 初始化, 还需要 对 数组元素 进行初始化
	private static void initG() {
		for (int i = 0; i < n + 1; i++) {
			g[i] = new ArrayList<Integer>();
		}
	}
}

summary

01 Triangular area warm-up without programming
02 Cube changes itself simple enumeration
03 Sanyang Xianrui simple enumeration tips
*04 Circulation section length has pit logic
05 Nine array scores are all arranged with mixed numbers
06 Addition changes multiplication simple enumeration tips
07 cards Type number recursion
08 Drink redemption simulation
****09 Base dice recursion-movement-matrix fast power
10 Tree of life Java recursion up to 10,000 layers

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/108123806