2019全国高校计算机程序设计挑战赛Java

1.求小于n的立方数

import java.util.Scanner;

public class 编程一 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(get(n));
    }

    private static int get(int n) {
        int i = 1;
        int curNum = 1;
        while (curNum <= n) {
            i++;
            curNum = i * i * i;
        }
        return i - 1;
    }
}

2.判断一共有几组连续三个数是合数

import java.util.Scanner;

public class 编程二 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] data = new int[n];
        for (int i = 0; i < n; i++) {
            data[i] = sc.nextInt();
        }
        System.out.println(get(data));
    }

    private static int get(int[] data) {
        boolean[] res = isHeshu(data);
        int result = 0;
        int count = 0;
        for (int i = 0; i < data.length; i++) {
            if (res[i]) {
                count++;
                if (count >= 3) {
                    result++;
                }
            } else {
                count = 0;
            }
        }
        return result;
    }
    private static boolean[] isHeshu(int[] data) {
        boolean[] res = new boolean[data.length];
        for (int i = 0; i < res.length; i++) {
            for (int j = 2; j <= Math.sqrt(data[i]); j++) {
                if (data[i]%j == 0) {
                    res[i] = true;
                    break;
                }
            }
        }
        return res;
    }
}

3.判断字符串是不是可以被消消乐

import java.util.Scanner;

public class 编程三 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        System.out.println(get(str));
    }

    private static String get(String str) {
        boolean[] index = new boolean[str.length()];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (index[i]) {
                continue;
            }
            for (int j = i + 1; j < str.length(); j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    index[i] = true;
                    index[j] = true;

                }
            }
            if (!index[i]) {
                sb.append(str.charAt(i));
            }

        }
        return sb.length() != 0 ? sb.toString() : "YES";
    }
}

4.找到滑动窗口的差的最大值

import java.util.LinkedList;
import java.util.Scanner;


public class 编程四 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        LinkedList<group> minList = new LinkedList<>();
        LinkedList<group> maxList = new LinkedList<>();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int i = 0;
        int maxRes = 0;
        while (i < n) {
            int cur = sc.nextInt();
            while (!maxList.isEmpty() && maxList.getLast().value < cur) {
                maxList.removeLast();
            }
            while (!maxList.isEmpty() && maxList.peek().index + m <= i) {
                maxList.removeFirst();
            }
            maxList.addLast(new group(cur, i));
            while (!minList.isEmpty() && minList.getLast().value > cur) {
                minList.removeLast();
            }
            while (!minList.isEmpty() && minList.peek().index + m <= i) {
                minList.removeFirst();
            }
            minList.addLast(new group(cur, i));
            maxRes = Math.max(maxList.peek().value - minList.peek().value, maxRes);
            i++;
        }
        System.out.println(maxRes);

    }
    static class group{
        int value;
        int index;
        group(int value, int index) {
            this.value = value;
            this.index = index;
        }
    }
}

5.将货物由大到小排列

import java.text.DecimalFormat;
import java.util.*;

public class 编程五 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        HashMap<Character, wuzi> hashMap = new HashMap<Character, wuzi>();
        for (int i = 0; i < n; i++) {
            char m = sc.findInLine(".").charAt(0);
            int a = sc.nextInt();
            double b = sc.nextDouble();
            sc.nextLine();
            wuzi cur = new wuzi(m, a * b);
            if (hashMap.containsKey(m)) {
                hashMap.put(m, new wuzi(m, hashMap.get(m).value + a * b));
            } else {
                hashMap.put(m, new wuzi(m, a * b));
            }


        }
        wuzi[] res = new wuzi[hashMap.size()];
        int aasdf = 0;
        for (Map.Entry<Character, wuzi> entry : hashMap.entrySet()) {
            res[aasdf++] = entry.getValue();
        }
        Arrays.sort(res, new Comparator<wuzi>() {
            @Override
            public int compare(wuzi o1, wuzi o2) {
                if (o2.value - o1.value < 0){
                    return -1;
                } else if (o2.value - o1.value == 0) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i].toString());
        }
    }
    static class wuzi implements Comparable<wuzi>{
        char c;
        double value;
        wuzi(char c, double value) {
            this.c = c;
            this.value = value;
        }

        @Override
        public int compareTo(wuzi o) {
            return this.c - o.c;
        }
        @Override
        public String toString() {
            return c + " " +String.format("%.2f", value);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/shihaibi/p/11877555.html