当前位置:网站首页>Niuke - real exercise-01

Niuke - real exercise-01

2022-06-21 14:41:00 Renshanren

Cattle guest - Real exercises -01

58 Same city 2020 Campus recruitment written test - Back end

The first question is

image-20220222123327338image-20220222123415027

The first question is the code implementation

import java.util.*;

public class Main{
    
    public static void main(String[] args){
    
        Scanner sc = new Scanner(System.in);
        
        String str = sc.nextLine();
        String[] strList = str.split(",");
        int count = 1;
        
        for(int i = 1;i < strList.length;i++){
    
            if(!strList[i].equals(strList[i-1])){
    
                count++;
            }
        }
        
        System.out.print(count);
    }
}

The second question is

image-20220222123603359

image-20220222123631119

image-20220222123651847

Question 2 code implementation

import java.util.*;

public class Main{
    
    public static void main(String[] args){
    
        Scanner sc = new Scanner(System.in);
        
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] list = new int[m][n];
        
        for(int i = 0;i < m;i++){
    
            for(int j = 0;j < n;j++){
    
                list[i][j] = sc.nextInt();
            }
        }
        
        int[][] dp = new int[m][n];
        
        dp[0][0] = list[0][0];
        for(int i = 1;i < n;i++){
    
            dp[0][i] = dp[0][i-1] + list[0][i];
        }
        for(int i = 1;i < m;i++){
    
            dp[i][0] = dp[i-1][0] + list[i][0];
        }
        
        for(int i = 1;i < m;i++){
    
            for(int j = 1;j < n;j++){
    
                dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + list[i][j];
            }
        }
        
        //for(int i = 0;i < m;i++){
    
        // for(int j = 0;j < n;j++){
    
        // System.out.print(dp[i][j]);
        // }
        // System.out.println();
        //}
        
        System.out.print(dp[m-1][n-1]);
    }
}

Third question

image-20220222123812163

image-20220222123848454

image-20220222123920115

Third question code implementation

import java.util.*;

public class Main{
    
    public static void main(String[] args){
    
         Scanner sc = new Scanner(System.in);
        
        int m = sc.nextInt();
        int[] list = new int[m];
        
        //  Fill in the child's rating 
        for(int i = 0;i < m;i++){
    
            list[i] = sc.nextInt();
        }
        
        //  Make one tx Represents the smallest biscuit that each child should receive .
        int[] childs = new int[m];
        int minSum = 0;
        childs[0] = 1;
        
        //  greedy , First determine the score of the left comparison , Then determine the score of the comparison on the right , The score on the left should be compared with that on the right .
        for(int i = 1;i < m;i++){
    
            if(list[i] > list[i-1]){
    
                childs[i] = childs[i-1] + 1;
            } else{
    
                childs[i] = 1;
            }
        }
        
        for(int i = m-2;i >= 0;i--){
    
            if(list[i] > list[i+1]){
    
                childs[i] = Math.max(childs[i],childs[i+1] + 1);
            }
        }
        
        for(int i = 0;i < m;i++){
    
            minSum += childs[i];
        }
        
        System.out.print(minSum);
    }
}

summary

58 Yes. ACM Pattern , You need to import the library yourself , And you need to write your own input , I'm used to making force buckles, but I don't adapt to it . Then the question is very similar to the question practiced by Li Kou , I feel I have done everything , Good luck is also possible .

原网站

版权声明
本文为[Renshanren]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221413312052.html