当前位置:网站首页>21st day of written test mandatory training

21st day of written test mandatory training

2022-07-24 03:18:00 Curry won't throw three points

choice question

  • Although the single linked list can be located by binary search , But the insertion may only be head or tail , So you still need to traverse

 

 

  • Loading factor = Valid elements / The length of the array , Load factor increases , There should be more effective elements , Or the array length becomes smaller , That one will increase the probability of conflict  

 

 

Programming questions

 

  • This problem is to find rules , After reading one number at a time , Figure out how he passed k Position after the second shuffle , Use only one length 2n Arrays are used to output According to the position of the current number , You can calculate the position after a shuffle if the current number is less than or equal to n( In the left hand ), Then the position of his next appearance is 2* The current position The corresponding current position + n( In the right hand ) The card of , Then the position of his next appearance is 2* The current position + 1
package days.day21;
import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t>0){
            int n=sc.nextInt();
            int k=sc.nextInt();
            int arr[]=new int[2*n];
            for(int i=0;i<2*n;i++){
                arr[i]=sc.nextInt();
            }
            sort(arr,k,n);
            t--;
        }
    }
    public static void sort(int []arr,int k,int n){
        for(int i=0;i<k;i++){
            int newarr[]=new int[2*n];
            for(int j=0;j<n;j++){
                newarr[2*j]=arr[j];
                newarr[2*j+1]=arr[j+n];
            }
            arr=newarr;
        }
        for(int i=0;i<2*n;i++){
            System.out.print(arr[i]);
            if(i!=2*n-1){
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

 

  • This problem is completely handled according to the situation , First, it is divided into two major situations 1 Characters less than or equal to 4( There will be no skipping to the last page , And the question on the first page , There is no need to deal with the problem of changing pages )2 Characters greater than or equal to 4
  • Characters greater than or equal to 4 There are many kinds of situations 1 Skip from the first page to the last 2 Skip from the last page to the last page 3 Refresh the first line The whole page moves down one grid 4 Refresh the last line in , The whole page moves up one space 5 Normal cursor down , Do not refresh page 6 Normal cursor moves up , Do not refresh page  
package days.day21;

import java.util.*;
public class Main2 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String str = sc.next();
        char arr[] = str.toCharArray();
        int now = 1;
        int list = 1;
        if (n <= 4) {
            for (int i = 0; i < arr.length; i++) {
                if (now == 1 && arr[i] == 'U') {
                    now = n;
                } else if (now == n && arr[i] == 'D') {
                    now = 1;
                } else if (arr[i] == 'U') {
                    now--;
                } else if (arr[i] == 'D') {
                    now++;
                }
            }
            for (int i = 1; i <= n; i++) {
                System.out.print(i + " ");
            }
            System.out.println();
            System.out.println(now);
        } else {
            for (int i = 0; i < arr.length; i++) {
                // When the cursor is on the first line 
                // If you go up, you need to change to the last page 
                if (list == 1 && now == 1 && arr[i] == 'U') {
                    list = n - 3;
                    now = n;
                // When the cursor is on the last line 
                // If you go down, you need to change to the first page 
                } else if (list == n - 3 && now == n && arr[i] == 'D') {
                    list = 1;
                    now = 1;
                } else if (list != 1 && now == list && arr[i] == 'U') {
                    list--;
                    now--;
                } else if (list != n - 3 && now == list + 3 && arr[i] == 'D') {
                    list++;
                    now++;
                } else if (arr[i] == 'D') {
                    now++;
                } else if (arr[i] == 'U') {
                    now--;
                }
            }
            for (int i = list; i <= list + 3; i++) {
                System.out.print(i + " ");
            }
            System.out.println();
            System.out.println(now);
        }
    }
}

原网站

版权声明
本文为[Curry won't throw three points]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207222259179080.html