<Lanqiao Cup Software Competition>20 weeks of zero-based preparation--Week 5--Miscellaneous questions-2

Students who have signed up for the Blue Bridge Cup Software Competition in April next year, if you are a freshman with zero foundation and are currently confused and don’t know what to do, you can take a look at this blog series:Collection of 20 weeks of preparation
For the complete schedule of 20 weeks, please click:20-week plan
Post 1 blog every week , 20 weeks in total (readers can choose "normal" and "fast forward" at their own pace ).
Focus on answering questions 3 times a week
, on Wednesday, Friday and Sunday evenings on the QQ group:

Insert image description here

Round 4:  Game-2

0. Q&A last week

  How to convert pseudocode into Java language?
  How to convert C/C++, Java, and Python codes to each other?
  Answer: Use big data model to convert , for example Wen Xinyiyan 3.5 , is free.
  Big data models do a good job of converting codes.
Insert image description here

1. Lecture the topic carefully

  This week is still about miscellaneous topics!
  Do you feel that it is too slow? When will you learn data structures and algorithms? I want to learn right now: bisection, sorting, binary trees, DFS,...
  Don’t be anxious, the more miscellaneous questions you do, the more efficient and faster the subsequent learning will be!
  First, improve your coding ability! Strive to achieve this: write 20 lines of code at a time, without debugging it once!
  The second is algorithmic ability! Although the miscellaneous problems do not use classical algorithms, their problem-solving steps are actually algorithms, and sometimes they are no less difficult than classical algorithms.
  To give 6 detailed lecture questions, including simulation, construction, thinking, mathematics and other miscellaneous questions, ranging from easy to difficult. Everyone should do it by themselves first, and then look at the solution.

1.1 Pruning shrubs

Difficulty**
The 13th Provincial Competition in 2022:Link 1-Lanqiao OJ , Link 2-NewOj
[Solution] This is a thinking question. Since each shrub will be pruned to 0 in 2N days, it will not grow indefinitely. Among them, the i-th tree has i-1 trees on its left and n-i trees on its right. Alice goes back from the left and right, which takes 2i and 2(n-i-1) days respectively. The maximum value is the height. i starts from 0.
(1) C/C++ code

#include<bits/stdc++.h>
using namespace std;
int main() {
    
    
    int n;  cin >> n;
    for (int i = 0; i < n; i++)   cout << max(i, n - i - 1) * 2 << endl;
    return 0;
}

(2) python code

n = int(input())
for i in range (n): print(max(i,n-i-1)*2)

(3)java code

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
    
    
            System.out.println(Math.max(i, n - i - 1) * 2);
        }
    }
}

1.2 English number counting

See blog:English number counting
Difficulty ***

1.3 Rectangular splicing

The 13th Provincial Competition in 2022: Link-NewOJ
Difficulty*** Accurately Said, the difficulty level is 3.5 stars
[Solution] This question is a purely structural question, the thinking is simple, but the code It is relatively tedious and detailed, and the purpose is to assess coding ability.
  Three rectangles placed together, how many sides might they have? Readers can draw and observe on paper. If the three rectangles cannot match at all, it is an octagon; if they can completely match into a new rectangle, it is a 4-sided shape; in other cases, it is a 6-sided shape.
  This question only has 3 rectangles, so it is not complicated. Make any combination of 3 rectangles. Each rectangle can be placed horizontally or vertically, for a total of 48 situations. T = 1000 sets of tests, the total calculation amount is 1000×48, the calculation amount is very small and will not time out, so simply use the brute force method to combine all situations and take the minimum value.
(1) C/C++ code
  The following C++ code, lines 10 ~ 14, combines three rectangles. In lines 15 to 17, each rectangle can be placed horizontally or vertically.
  Line 18, if the side length of a rectangle is equal to the sum of the side lengths of the other two rectangles, then there are at most 6 sides. Then line 20, if the sides of the two rectangles are equal, then they have 4 sides. Please analyze the next few lines yourself.

#include<bits/stdc++.h>
using namespace std;
int a[3][2];
int main(){
    
    
    int T;    cin >> T;
    while(T--)    {
    
    
        for(int i = 0; i < 3; i++)
            cin >> a[i][0] >> a[i][1];
        int ans = 8;
        for(int i = 0; i < 3; i++)               //第1个矩形
            for(int j = 0; j < 3; j++)
                if(i != j)                       //第2个矩形
                    for(int k = 0; k < 3; k++)
                        if(k != i && k != j)     //第3个矩形
                            for(int ii = 0; ii <= 1; ii++){
    
               //第1个有横竖两种摆法
                                for(int jj = 0; jj <= 1; jj++){
    
           //第2个横竖摆
                                    for(int kk = 0; kk <= 1; kk++){
    
       //第3个横竖摆
                                        if(a[i][ii] == a[j][jj] + a[k][kk]){
    
     
                                            ans = min(ans, 6);
                                            if(a[j][1-jj] == a[k][1-kk])
                                                ans = min(ans, 4);
                                        }
                                        if(a[i][ii] == a[j][jj] || a[j][jj] == a[k][kk])
                                            ans = min(ans, 6);
                                        if(a[i][ii] == a[j][jj] && a[j][jj] == a[k][kk])
                                            ans = min(ans, 4);
                                    }
                                }
                            }
        cout<<ans<<endl;
    }
    return 0;
}

(2) python code

def check1(x1,x2,x3):
    if x1>=x2 and x1>=x3:
        if x1==x2+x3 and a[2]+a[3]-x2==a[4]+a[5]-x3:  return True
    if x2>=x1 and x2>=x3:
        if x2==x1+x3 and a[0]+a[1]-x1==a[4]+a[5]-x3:  return True
    if x3>=x1 and x3>=x2:
        if x3==x1+x2 and a[0]+a[1]-x1==a[2]+a[3]-x2:  return True
    return False
def check2(x1,x2,x3):
    if x1>=x2 and x1>=x3:
        if x1==x2+x3:    return True
    if x2>=x1 and x2>=x3:
        if x2==x1+x3:    return True
    if x3>=x1 and x3>=x2:
        if x3==x1+x2:    return True
    return False
     
T = int(input())
for t in range(T):
    a=list(map(int,input().split()))
    ans=8
    for i in range(0,2):              #第1个矩形
        for j in range(2,4):          #第2个矩形
            for k in range(4,6):      #第3个矩形
                x1,x2,x3 = a[i],a[j],a[k]
                if x1==x2 and x2==x3:          ans = min(ans,4)
                if check1(x1,x2,x3):           ans = min(ans,4)
                if x1==x2 or x1==x3 or x2==x3: ans = min(ans,6)
                if check2(x1,x2,x3):           ans = min(ans,6)
    print(ans)

  Kayka netizen wrote the following code, which is simpler.

T = int(input())
for t in range(T):
    a=list(map(int,input().split()))
    ans=8
    for i in range(0,2):              #第1个矩形
        for j in range(2,4):          #第2个矩形
            for k in range(4,6):      #第3个矩形
                x1,x2,x3 = a[i],a[j],a[k]
                d=a[0]+a[1]-x1
                b=a[2]+a[3]-x2
                c=a[4]+a[5]-x3
                tmp=[(a[i],d),(a[j],b),(a[k],c)]
                tmp.sort()
                x,y,z=tmp
                if x[0]==y[0] and y[0]==z[0]:# 三边相等的情况
                    ans=4 
                if x[0]+y[0]==z[0]:   #最小的两边等于第大边的两种情况
                    if x[1]==y[1]:    #第一种
                        ans=4
                    else:             #第二种
                        ans=min(ans,6)
                if x[0]==y[0]or y[0]==z[0]:
                    ans=min(ans,6)
        if ans == 4:
            break
                
    print(ans)

(3)java code

import java.util.*;
import java.io.*; 
public class Main {
    
     
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for(int _t = 1; _t <= T; _t++){
    
    
            int[][] a = new int[3][2];
            int ans = 8;
            for(int i = 0; i < 3; i++) {
    
    
                a[i][0] = in.nextInt();
                a[i][1] = in.nextInt();
            }
            //枚举第一个矩形下标为i,第二个矩形下标为j,第三个矩形下标为k
            for(int i = 0; i < 3; i++)
                for(int j = 0; j < 3; j++)
                    if(i != j)
                        for(int k = 0; k < 3; k++)
                            if(i != k && j != k)
                            //枚举三个矩形的两条边
                                for(int ii = 0; ii <= 1; ii++)
                                    for(int jj = 0; jj <= 1; jj++)
                                        for(int kk = 0; kk <= 1; kk++) {
    
    
                                            if(a[i][ii] == a[j][jj])//6条边的情况1
                                                ans = Math.min(ans, 6);
                                            if(a[i][ii] == a[j][jj] && a[i][ii] == a[k][kk])//4条边的情况1
                                                ans = Math.min(ans, 4);
                                            //枚举仅考虑a[i][ii] 与 a[j][jj] + a[k][kk]的关系
                                            if(a[i][ii] == a[j][jj] + a[k][kk])  {
    
    
                                                ans = Math.min(ans, 6);     //6条边的情况2
                                                if(a[j][1 - jj] == a[k][1 - kk])    //4条边的情况2
                                                    ans = Math.min(ans, 4);
                                            }
                                    }
            System.out.println(ans);
        }
    }
}

1.4 Minimum weight

The 12th Lanqiao Cup Provincial Competition in 2021: Link 1-Lanqiao OJ
Difficulty***< a i=3> [Solution] This is a pattern-finding question.   The question requires giving a minimum number of integers (weights), and obtaining integers from 1 to N through addition and subtraction. Anyone who is familiar with binary knows that using 1, 2, 4, 8, 16,... these multiples of 2 can be combined and added to obtain any integer. However, the weights in this question can not only be added, but can also be placed on both sides of the scale and subtracted to obtain a new weight. For example, when N = 3, the weight can be {1, 2}, {1, 3}, or {2, 3}.   The thinking of this question is a bit difficult. If readers have done this question in advance, the thinking process may be more cumbersome. A concise reasoning method is given below.   Suppose the current weight weighing range is 1 ~ R. Add a weight w, and it is required not to repeat the weighing that can be obtained before adding w, then w will be a very large weight. The new weighing range is:     ˜ ˜ ˜{1, 2, …, R, w-R, w-R+1,…, w, w+1, w+2, …, w+R }   ˜Because it is continuous from R to w-R, there is w-R = R+1, that is, w = 2R+1. That is to say, if the current weighing range is 1 ~ R, then adding a weight w = 2R+1 can be extended to the new weighing range R’ = w+R = 3R+1.   Calculation in the following list, the "original weight, original weighing range" in each row is the "new weight, new weighing range" in the previous row.   This table gives an implementation method with the least weight. Although there may be other implementation methods, this implementation method expands the new R' to the greatest extent and is an optimal solution. .   The calculation method can be obtained according to this table: (1) The weight increases by a multiple of 3; (2) Each time a weight is added, the weighing range increases to R’ = 3R+1.   R increases by 3 times, which is faster than the binary doubling. When N = 1 0 9 10^9







Insert image description here


109time, calculation amount l o g 3 N log_3N log3N < l o g 2 N log_2N log2N = 30。

(1) C/C++ code

#include <stdio.h>
int main() {
    
    
    int N;
    scanf("%d", &N);
    int R = 1;
    int cnt = 1;
    while (R < N) {
    
    
        R = R * 3 + 1;
        cnt++;
    }
    printf("%d", cnt);
    return 0;
}

(2) python code

N = int(input())
R = 1 
cnt = 1
while R < N:
    R = R*3 + 1
    cnt += 1
print(cnt)

(3)java code

import java.util.*;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int R = 1;
        int cnt = 1;
        while (R < N) {
    
    
            R = R * 3 + 1;
            cnt++;
        }
        System.out.println(cnt);
    }
}

1.5 Hive

The 13th Provincial Competition in 2022: [Link 1-Lanqiao OJ]
Difficulty****< a i=3> [Solution] This question is a construction question with two test points: coordinate conversion and distance calculation.   Hive has 6 directions, which looks complicated, but in fact walking is very simple. For example, in the example, walk from B to C, C is at the lower right of B, and B just needs to keep going to the right. Go down, and do not exceed the row and column of C. No matter how you go, you will be able to reach C in the minimum number of steps.   The difficulty of this question is the processing of coordinates. If it is a simple rectangular coordinate system, it is easy to calculate. This question is about a hexagonal honeycomb. Can the center point of each honeycomb be converted into rectangular coordinates? Express the relationship of the hives with the following rectangular coordinates:   ˜ Center point O, the coordinates of the corresponding 6 hives are (-2, 0), (-1, 1), (1, 1), (2, 0), (1, -1), (-1, -1), represented by xdir[] and ydir[] in the following code.   First calculate the starting point coordinates (x1, y1) and the end point coordinates (x2, y2). How to calculate the number of steps from starting point to end point? Since the coordinates of the honeycomb are rather strange, we cannot directly use the "Manhattan distance [Manhattan distance, also known as taxi distance: the distance between two points is equal to the distance in the x direction plus the distance in the y direction, that is, |x1-x2| + | y1-y2|.]" calculation. If readers have already done this question, they may have used various complex judgments to calculate it. Here is a simple and clever method.   ˆ The absolute value of the coordinate difference dx = |x1-x2|, dy = |y1-y2|, the following conclusions are reached:   ˆ 1. If dx ≥ dy , then the minimum number of steps is (dx+dy)/2, that is, walk sideways first, and then walk diagonally;   2. If dx < dy, just walk diagonally all the time, the minimum number of steps It's dy.



Insert image description here




(1) C/C++ code
  There is one thing you need to pay attention to in the code, that is, long long should be used for coordinate values. If int is used, it will overflow.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll xdir[] = {
    
    -2,-1,1,2, 1,-1};  //横向
ll ydir[] = {
    
     0, 1,1,0,-1,-1};  //纵向
void walk(ll d, ll q, ll &x, ll &y){
    
    
    x += xdir[d] * q;
    y += ydir[d] * q;           //引用传参,返回坐标值(x,y)
}
int main(){
    
    
    ll d1,p1,q1,d2,p2,q2;
    cin>>d1>>p1>>q1>>d2>>p2>>q2;
    ll x1 = 0, y1 = 0;           //计算起点坐标(x1, y1)
    walk(d1,p1,x1,y1);           //先走第1个方向
    walk((d1 + 2) % 6,q1,x1,y1); //再走第2个方向
    ll x2 = 0, y2 = 0;           //计算终点坐标(x2, y2)
    walk(d2,p2,x2,y2);
    walk((d2 + 2) % 6,q2,x2,y2);
    ll dx = abs(x1 - x2), dy = abs(y1 - y2);
    if (dx >= dy) cout << (dx+dy)/2;      //先横走,再斜着走
    else          cout << dy;              //一直斜着走就行了
}

(2) python code

xdir = [-2,-1,1,2, 1,-1]
ydir = [ 0, 1,1,0,-1,-1]
def walk(d, q,x,y):  
    x += xdir[d]*q
    y += ydir[d]*q
    return x,y
d1,p1,q1,d2,p2,q2 = map(int,input().split())
x1, y1 = walk(d1,p1,0,0)
x1, y1 = walk((d1 + 2) % 6, q1,x1,y1)
x2, y2 = walk(d2,p2,0,0)
x2, y2 = walk((d2 + 2) % 6, q2,x2,y2)
dx,dy = abs(x1 - x2), abs(y1 - y2);
if (dx >= dy): print((dx+dy)//2)     #先横走,再斜着走
else:          print(dy)             #一直斜着走 

(3)java code

import java.util.*;
public class Main {
    
    
    static long[] xdir = {
    
    -2, -1, 1, 2, 1, -1}; //横向
    static long[] ydir = {
    
    0, 1, 1, 0, -1, -1}; //纵向
    static void walk(int d, long q, long[] pos) {
    
    
        pos[0] += xdir[d] * q;
        pos[1] += ydir[d] * q;
    }
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int d1 = scanner.nextInt();
        long p1 = scanner.nextLong();
        long q1 = scanner.nextLong();
        int d2 = scanner.nextInt();
        long p2 = scanner.nextLong();
        long q2 = scanner.nextLong();
        long[] pos1 = {
    
    0, 0};
        walk(d1, p1, pos1);
        walk((d1 + 2) % 6, q1, pos1);
        long[] pos2 = {
    
    0, 0};
        walk(d2, p2, pos2);
        walk((d2 + 2) % 6, q2, pos2);
        long dx = Math.abs(pos1[0] - pos2[0]);
        long dy = Math.abs(pos1[1] - pos2[1]);
        if (dx >= dy)  System.out.println((dx + dy) / 2);
        else           System.out.println(dy);        
    }
}

1.6 Cube surface distance

See blog:Cube surface distance
Difficulty *****

2. Practice questions

  Let’s continue to answer questions. Last week’s question link, please continue:

  Simulation questions from Blue Bridge Question Bank-Easy
  Simulation questions from Blue Bridge Question Bank-Medium
  Simulation questions from Blue Bridge Question Bank-Hard

  The enumeration questions from the Blue Bridge question bank - easy
  The enumeration questions from the Blue Bridge question bank - medium
  The enumeration questions from the Blue Bridge question bank - difficult

  Recursion questions from Blue Bridge question bank

Guess you like

Origin blog.csdn.net/weixin_43914593/article/details/134209848