2020 Blue Bridge Cup Java Group A Zhenti Analysis

The competition is approaching. I have done a lot of practice every day. It is time to do the real questions and practice my hands. I will fill in the blanks today and fill in the bigger questions tomorrow.

house number production

image-20220329215115918

Topic link: http://oj.ecustacm.cn/problem.php?id=1508

Sign-in questions, traverse all directly, and find the statistics with 2.

Answer: 624

package year2020A;

/**
 * http://oj.ecustacm.cn/problem.php?id=1508
 * 
 * @author Jia
 *
 */
public class exercise1 {
    
    
	public static void main(String[] args) {
    
    
		int ans = 0;
		for (int i = 1; i <= 2020; i++) {
    
    
			int j = i;
			while (j > 0) {
    
    
				if (j % 10 == 2) {
    
    
					ans++;
				}
				j = j / 10;
			}
		}
		System.out.println(ans);
	}
}

abbreviated fraction

image-20220329215254823

Topic link: http://oj.ecustacm.cn/problem.php?id=1509

It's still the same, just traverse all of them directly, and then judge whether the value of the greatest common divisor of the numerator and denominator is 1. Be careful not to use prime numbers to spell directly. I did it by directly calculating the prime numbers for the first time, and then I found 8/9 also satisfies the condition, but both are not prime numbers

Answer: 2481215

package year2020A;

/**
 * http://oj.ecustacm.cn/problem.php?id=1509
 * 
 * @author Jia
 *
 */
public class exercise2 {
    
    
	public static void main(String[] args) {
    
    
		int ans = 0;
		int end = 2021;
		for (int i = 1; i < end; i++) {
    
    
			for (int j = 1; j < end; j++) {
    
    
				if (gcd(i, j) == 1) {
    
    
					ans++;
				}
			}
		}
		System.out.println(ans);
	}

	/**
	 * 求最大公约数
	 * 
	 * @param i
	 * @param j
	 * @return
	 */
	private static int gcd(int i, int j) {
    
    
		return i % j == 0 ? j : gcd(j, i % j);
	}

}

serpentine fill

image-20220329215454529

Topic link: http://oj.ecustacm.cn/problem.php?id=1510

To tell the truth, this question was done by looking for a pattern. I couldn't do it at first, and then I calculated it by hand. The formula in line 22

Then the code word is to find out the rules for a while later, (1,1) in the third snake shape, (2,2) in the fifth snake shape, (3,3) in the seventh snake shape shape, and so on

Answer: 761

package year2020A;

/**
 * http://oj.ecustacm.cn/problem.php?id=1510
 * 
 * @author Jia
 *
 */
public class exercise3 {
    
    
	public static void main(String[] args) {
    
    
		int ans = 0;
		int val = 0;
		for (int i = 1; i <= 39; i++) {
    
    
			for (int j = 1; j <= i; j++) {
    
    
				val++;
				if (i == 39 && j == 20) {
    
    
					ans = val;
				}
			}
		}
		System.out.println(ans);
//		System.out.println(19 * 39 + 20);
	}
}

seven-segment code

image-20220329220007257

Topic link: http://oj.ecustacm.cn/problem.php?id=1511

Uh, I thought this question was quite simple, so I found it from the direct hands, and then gave up hhhh

Then the idea behind is to record the connection relationship of all edges, then list all possible choices, and then judge whether this choice can be established

If you list all the options, it is the subset tree of the backtracking method. Then, because the size is different each time, you need to find the subset tree with dfs multiple times, and then judge whether the conditions are met after reaching the required size (in fact, you can also Once bfs, judge every time you enter, but I don’t feel like it, it’s better to write a template directly)

Then label each edge as follows to establish a matrix storage edge connection relationship. When judging whether a given subset meets the conditions, it is necessary to see how many connection points these edges have, for example, for 0, 1, 2, 3 There are 3 connection points. If you list a few more examples, you will find that for n edges, there will be at least n-1 connection points, so the relationship between how to judge whether the subset satisfies the condition comes out (here is a kind of Exceptions, such as 0,1,6,5 have 4 connection points, but it is so-called), in addition to this, it is necessary to make sure that each edge has at least one connection with other edges. In the code, I use set to judge

image-20220329220810884

Answer: 80

package year2020A;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * http://oj.ecustacm.cn/problem.php?id=1511
 * 
 * @author Jia
 *
 */
public class exercise4 {
    
    
	static int[][] link = {
    
     {
    
     0, 1, 0, 0, 0, 1, 0 }, {
    
     1, 0, 1, 0, 0, 0, 1 }, {
    
     0, 1, 0, 1, 0, 0, 1 },
			{
    
     0, 0, 1, 0, 1, 0, 0 }, {
    
     0, 0, 0, 1, 0, 1, 1 }, {
    
     1, 0, 0, 0, 1, 0, 1 }, {
    
     0, 1, 1, 0, 1, 1, 0 }, };
	static int ans = 0;
	static int[] arr = {
    
     1, 2, 3, 4, 5, 6, 7 };
	static ArrayList<Integer> nums = new ArrayList<>();// 记录组合

	public static void main(String[] args) {
    
    
		for (int size = 1; size <= 7; size++) {
    
    
			dfs(size, 0);
		}
		System.out.println(ans);
	}

	/**
	 * 回溯法
	 * 
	 * @param size
	 * @param index
	 */
	private static void dfs(int size, int index) {
    
    
		if (nums.size() == size) {
    
    
			check();
		} else {
    
    
			for (int i = index; i < link.length; i++) {
    
    
				nums.add(i);
				dfs(size, i + 1);
				nums.remove(nums.size() - 1);
			}
		}
	}

	/**
	 * 判断这种选择是否满足条件
	 */
	private static void check() {
    
    
		int num = nums.size();
		Set<Integer> set = new HashSet<>();// 使用hashset是为了确保所有的边都和其他有相连
		for (int i = 0; i < nums.size() - 1; i++) {
    
    
			for (int j = i + 1; j < nums.size(); j++) {
    
    
				if (link[nums.get(i)][nums.get(j)] == 1) {
    
    
					num--;
					set.add(i);
					set.add(j);
				}
			}
		}
		// 节点剩余连接数小于1并且所有的边都相连着
		if (num <= 1 && (set.size() == nums.size() || nums.size() == 1)) {
    
    
			ans++;
		}
	}

}

Plane segmentation

image-20220329221115594

Topic link: http://oj.ecustacm.cn/problem.php?id=1512

This question is also a question of finding rules. The rules I finally found are as follows:

  • For a circle, if there are n intersections with other circles, the plane can be divided into n additional parts.
  • For a straight line, if there are n intersections with other graphics, the plane can be additionally divided into n+1 parts

It may be difficult to understand directly by looking at it directly. It is recommended to first push it by hand to find the rules and then come over to see it.

Then we must make the circle and the line have more intersections.

  • We first calculate the circle. The first circle directly divides the plane into two parts. The i-th circle can have (i-1) * 2 intersection points with the previous circle, that is, it can be additionally divided (i-1) * 2 areas
  • Then calculate the straight line. The first straight line has at most 40 intersections with the circle, and 40 additional areas can be divided. The i-th straight line can have 40 + ( i − 1 ) 40+(i-1) with the circle and the straight line.40+(i1 ) an intersection. That is, an additional 40 + ( i − 1 ) + 1 40+(i-1)+1can be divided40+(i1)+1 area

Answer: 1391

package year2020A;

/**
 * http://oj.ecustacm.cn/problem.php?id=1512
 * 
 * @author Jia
 *
 */
public class exercise5 {
    
    
	public static void main(String[] args) {
    
    
        // 算所有圆可以分成的平面数
		int ans = 2;
		for (int i = 2; i <= 20; i++) {
    
    
			ans += (i - 1) * 2;
		}
        // 算所有边可以分的平面数
		ans += 40;
		for (int i = 2; i <= 20; i++) {
    
    
			ans += (i - 1) + 40 + 1;
		}
		System.out.println(ans);
	}
}

Guess you like

Origin blog.csdn.net/qq_46311811/article/details/123833997