码迷,mamicode.com
首页 > 其他好文 > 详细

58笔试-忘记题目

时间:2019-09-23 11:47:02      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:+=   oid   ann   div   res   忘记   ++   inpu   hashset   

第一题

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        String[] arr = str.split(",");
        Set<String> set = new HashSet<>();
        for (int i = 0; i < arr.length; i++) {
            set.add(arr[i]);
        }
        System.out.println(set.size());

        input.close();
    }
}

 

第二题

import java.util.Scanner;

public class Main2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = input.nextInt();
        }

        int[] left = new int[n];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > arr[i - 1]) {
                left[i] = left[i - 1] + 1;
            }
            if (arr[i]==arr[i-1]){
                left[i]=left[i-1];
            }
        }

        int[] right = new int[n];
        for (int i = arr.length - 2; i > 0; i--) {
            if (arr[i] > arr[i + 1]) {
                right[i] = right[i + 1] + 1;
            }
            if (arr[i] == arr[i + 1]) {
                right[i] = right[i + 1];
            }
        }

        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            if (left[i] >= right[i]) {
                result[i] = left[i];
            } else {
                result[i] = right[i];
            }
        }

        int sum = n;
        for (int i = 0; i < n; i++) {
            sum += result[i];
        }

        System.out.println(sum);


        input.close();
    }
}

 

第三题

import java.util.Scanner;

public class Main3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();

        int[][] arr = new int[a][b];

        int[][] result = new int[a][b];

        for (int i = 0; i < a; i++) {
            for (int j = 0; j < b; j++) {
                arr[i][j] = input.nextInt();
            }
        }
        
        
        result[0][0] = arr[0][0];
        for (int i = 1; i < a; i++) {
            result[i][0] = result[i - 1][0] + arr[i][0];
        }

        for (int i = 1; i < b; i++) {
            result[0][i] = result[0][i - 1] + arr[0][i];
        }

        for (int i = 1; i < a; i++) {
            for (int j = 1; j < b; j++) {
                result[i][j] = Math.min(result[i][j - 1], result[i - 1][j]) + arr[i][j];
            }
        }

        System.out.println(result[a - 1][b - 1]);

        input.close();

    }
}

 

58笔试-忘记题目

标签:+=   oid   ann   div   res   忘记   ++   inpu   hashset   

原文地址:https://www.cnblogs.com/GG-Bond/p/11571393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!