②【Java Group】Analysis of the real questions of the Blue Bridge Cup Provincial Competition [Revitalize China] [Sort of Three Parts] Continuously updating...

insert image description here

Personal brief introduction: New star creator in the Java field; Alibaba cloud technology blogger, star blogger, expert blogger; on the way of learning Java, recording the learning process~ Personal homepage: .29.'s blog
Learning community
: Go in and have a stroll~

insert image description here

Blue Bridge Cup real questions--continuously updated...





1. Revitalize China


题目描述

insert image description here

Text version:
Xiao Ming participated in the school's fun sports meeting, one of which was: jumping grid.

There are some grids drawn on the ground, and a word is written in each grid, as shown below:

Start from me to revitalize,
I start to revitalize,
start to revitalize China,
start to revitalize China

During the competition, first stand in the grid with the word "From" in the upper left corner, and you can jump to the adjacent grid horizontally or vertically, but you cannot jump to the diagonal grid or other positions. Have to jump to the end of the word "Hua".

The route to be skipped just constitutes the sentence "Rejuvenate China starting from me".

Please help Xiao Ming calculate how many possible jumping routes he has in total?


解题思路:
To solve the problem with the help of recursive thinking, there are only two options for the
first grid jump:
①jump to the right
;
The two parameters represent the direction of the jumping grid. The coordinates corresponding to the parameters (0, 0) are used as the starting position, and the selection of all jumping grids is traversed recursively. When the x-axis subscript reaches 4, or the Y-axis subscript reaches 3, It can be identified as a route and return 1 as the accumulated number of times.


解题代码:

public class 振兴中华 {
    
    
	public static void main(String[] args) {
    
    
		int answer = dfs(0,0);
		System.out.print(answer);
	}
	public static int dfs(int x,int y) {
    
    
		if(x == 4 || y == 3) return 1; //当x轴下标到4,或Y轴下标到3时,就可以认定为一条路线
		return dfs(x+1,y) + dfs(x,y+1);//递归,开始向右跳得到的路线 + 看是向左跳得到的路线,从而获取的所有路线
	}

}




Two, three sorting (code fill in the blanks)


题目描述

insert image description here

Text version:
There are many classic algorithms for general sorting, such as quick sorting, Hill sorting, etc.

However, in practical applications, there are often more or less special requirements. We don't need to apply those classic algorithms, we can build better solutions according to the actual situation.

For example, to sort the numbers in an integer array:

Negative numbers are all on the left, positive numbers are on the right, and 0 is in the middle. Note that the characteristic of the problem is that order is not required in the negative area and the positive area. You can use this feature to end the battle with 1 linear scan!!

The following program achieves this goal.

Where x points to the integer array to be sorted, and len is the length of the array.

Please analyze the code logic and guess the code at the underline .

源代码(java)

import java.util.*;
public class Main
{
    
    
    static void sort(int[] x)
    {
    
    
        int p = 0;
        int left = 0;
        int right = x.length-1;
        
        while(p<=right){
    
    
            if(x[p]<0){
    
    
                int t = x[left];
                x[left] = x[p];
                x[p] = t;
                left++;
                p++;
            }
            else if(x[p]>0){
    
    
                int t = x[right];
                x[right] = x[p];
                x[p] = t;
                right--;
                //p++;                
            }
            else{
    
    
                ______________;
            }
        }
        
        show(x);
    }
    
    static void show(int[] x)
    {
    
    
        for(int i=0; i<x.length; i++)
        {
    
    
            System.out.print(x[i] + ",");
        }
        
        System.out.println();
    }
    
    public static void main(String[] args)
    {
    
    
        //int[] x = {25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0};
        sort(new int[]{
    
    -1,0,1,-2,0,2,-3,0,0,3,-4,-5,4,-6,0,5,6});
        sort(new int[]{
    
    -1,0,-1,-2,0,-2,-3,0,0,-3,-4,-5,-4,-6,0,-5,-6});
        sort(new int[]{
    
    1,0,1,2,0,2,3,0,0,3,4,5,4,6,0,5,6});
    }
}

解题思路:
This section of the code is to put the number < 0 to the left, and lthen padd one to both:

while(p<=right){
    
    
            if(x[p]<0){
    
             //若数小于0
            //将最左边数与当前数调换位置
                int t = x[left];
                x[left] = x[p]; 
                x[p] = t;
                //左边界后移一位
                left++;
                //p位置也后移一位
                p++;
            }

The next step is to exchange the position of the number >0 with the number on the right boundary (after the exchange, the p position does not move, and the right boundary moves forward one bit)
else if(x[p]>0){
    
    
                int t = x[right];
                x[right] = x[p];
                x[p] = t;
                right--;
                //p++;                
            }

Then the position of the line is naturally when the number of the p position = 0, it needs to be operated. At this time, the number at position p is neither greater than zero nor less than zero, so there is no need to move the left and right borders, and only need to move p one bit backward to continue the next judgment.

解题代码:

p++;




insert image description here

Guess you like

Origin blog.csdn.net/ebb29bbe/article/details/129234504