Codeing练习

输入输出

题1

package com.semanteme.oj;

import java.util.*;

public class Test01 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        // 注意 hasNext 和 hasNextLine 的区别
        try {
            while (in.hasNextLine()) { // 注意 while 处理多个 case

                String b = in.nextLine();

                String ans = getResult(b);
                System.out.println(ans);

                if(a-- == 0){
                    break;
                }
            }
        } catch (Exception e) {
            in.close();
        }
    }


    private static String getResult(String str) {

        if(str == null && str.length() == 0){
            return "";
        }

        Map<Integer, Integer> pointMap = new HashMap<>();
        int[] arr = new int[51];
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 4;
        pointMap.put(1, 1);
        pointMap.put(2, 2);
        pointMap.put(3, 4);

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {

            char c = str.charAt(i);
            if(isLetter1(c)){
                int point = 0;
                if (i > 2) {
                    point = arr[i - 2] + arr[i - 1] + arr[i - 3];
                    arr[i] = point;
                } else {
                    point = arr[i];
                }

                int x = (c - 'a' + point) % 26 + 'a';
                stringBuffer.append((char) x);
            }else if(isLetter2(c)){
                int point = 0;
                if (i > 2) {
                    point = arr[i - 2] + arr[i - 1] + arr[i - 3];
                    arr[i] = point;
                } else {
                    point = arr[i];
                }

                int x = (c - 'A' + point) % 26 + 'A';
                stringBuffer.append((char) x);
            }else {
                stringBuffer.append(c);
            }

        }

        return stringBuffer.toString();
    }

    private static boolean isLetter1(char c) {

        return c >= 'a' && c <= 'z';
    }

    private static boolean isLetter2(char c) {

        return c >= 'A' && c <= 'Z';
    }
}

题2

package com.semanteme.oj;

import java.util.*;

public class Test02 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String b = in.nextLine();

            // (10,1),(20,1),(30,2),(40,3)
            // (10,1),(10,1),(30,2),(40,3)
            String ans = getResult(b);
            System.out.println(ans);
        }
    }

    private static String getResult(String str) {

        List<Integer[]> list = new ArrayList<>();
        String[] split = str.split(",");


        for (int i = 0; i < split.length/2; i++) {

            int a = 0;
            int b = 0;
            if(split[2*i].startsWith("(")){
                a = Integer.parseInt(split[2*i].substring(1));
            }

            if(split[2*i + 1].endsWith(")")){
                b = Integer.parseInt(split[2*i + 1].substring(0, split[2*i].length() - 2));
            }

            list.add(new Integer[]{a, b, i});
        }
        Collections.sort(list, (o1, o2) -> {return o2[1] - o1[1] != 0 ? o2[1] - o1[1] : o1[2] - o2[2];});

        List<Integer[]> list2 = new ArrayList<>();
        Integer[] pre = list.get(0);
        list2.add(pre);
        for (int i = 1; i < list.size(); i++) {
            Integer[] cur = list.get(i);
            if(cur[0] == pre[0] && cur[1] == pre[1]){
                continue;
            }

            list2.add(cur);
            pre = cur;
        }

        StringJoiner stringJoiner = new StringJoiner(",");
        for (int i = 0; i < list2.size(); i++) {
            stringJoiner.add(list2.get(i)[0] + "");
        }
        return stringJoiner.toString();
    }
}

题3

package com.semanteme.oj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Test03 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a1 = in.nextLine();
            String b2 = in.nextLine();

            String ans = getResult1(a1, b2);
            System.out.println(ans);
        }
    }


    private static String getResult1(String a, String b) {

        int n = Integer.parseInt(b);
        char[] charArray = a.toCharArray();

        int[] min = new int[]{Integer.MAX_VALUE};


        List<Character> path = new ArrayList<>();
        boolean[] used = new boolean[charArray.length];
        StringBuffer stringBuffer = new StringBuffer();
        dst1(charArray, n, min, path, used);

        return "最小值" + min[0]+"";
    }

    private static void dst1(char[] charArray, int len, int[] min, List<Character> path, boolean[] used) {

        if(path.size() == charArray.length - len){

            StringBuffer stringBuffer = new StringBuffer();
            for (Character character : path) {
                stringBuffer.append(character);
            }

            int temp = Integer.parseInt(stringBuffer.toString());
            if(String.valueOf(temp).length() < charArray.length - len){
                return;
            }

            System.out.println(temp);
            if(min[0] > temp){
                min[0] = temp;
            }
            return;
        }

        for (int j = 0; j < charArray.length; j++) {

            if(!used[j]){
                used[j] = true;
                path.add(charArray[j]);
                dst1(charArray,len,min, path, used);
                path.remove(path.size() - 1);
                used[j] = false;
            }
        }
    }

    private static String getResult(String a, String b) {

        char[] charArray = a.toCharArray();

        Arrays.sort(charArray);

        return new String(charArray).substring(0, charArray.length - Integer.parseInt(b));

    }
}

你可能感兴趣的:(java,开发语言)