2020年全国高校计算机能力挑战赛初赛java组

前言

本人算法能力菜鸡水准,只会写写for循环。大佬手下留情。本次比赛编程题共有4题,但是第四题我没太看明白,而且这道题貌似我和其他人的不一样,具体也不知道咋回事。感觉又是当分母的一天。生活不易,咸鱼叹气。我的60块钱~~

题解

题目1

  1. 统计从1到N的整数中,所有立方值的平方根为整数的数的格式
    输入说明:整数N(N<10000)
    输出说明:符合条件的数的个数,如4^3= 64 = 8^2
    输入样例:10
    输出样例:3
    (说明:样例中符合条件的3个数是1、4、9)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int count = 0;
        for (int i = 1; i <= N; i++) {
            int temp = (int) Math.pow(i, 3);
            double sqrt = Math.sqrt(temp);
            String string = Double.toString(sqrt);
            int index = string.indexOf(".");
            String substring = string.substring(index + 1);
            if (substring.equals("0")){
                count++;
            }
        }
        System.out.println(count);
        scanner.close();
    }
}

题目2

  1. 给出长度N的各不相同整数组成的数组,求解2个数相加为M的情况个数
    输入说明:第一行,数组中元素个数N(N<1000),和值M;第二行,N个数组元素
    输出样例:8 10
    1 4 2 5 3 19 8 6
    输出样例:2
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        int[] array = new int[N];
        for (int i = 0; i < N; i++) {
            array[i] = scanner.nextInt();
        }
        int count = 0;
        int temp = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = temp; j < array.length; j++) {
                if (i == j)continue;
                if (array[i] + array[j] == M){
                    count++;
                }
            }
            temp++;
        }
        System.out.println(count);
        scanner.close();
    }
}

for循环优化

for (int i = 0; i < m; i++) {
    for (int j = i + 1; j < m; j++) {
        if (array[i] + array[j] == target){
            count++;
        }
    }
}

题目3

  1. 在一个由小写英文字母(a-z)组成的字符串中,查找最短子串,其头尾字母相同,且中间不包含该头尾字母,并输出最左边的该类子串。
    输入说明:待处理字串(长度≤ 200)
    输出说明:子串
    输入样例:adfdasjdoiasldhlfa
    输出样例:dfd

    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String str = scanner.next();
            String substring = null;
            for (int i = 0; i < str.length(); i++) {
                String tempStr = str.substring(i, i+1);
                for (int j = 1; j < str.length()-1; j++) {
                    if (i >= j)continue;
                    String tempStr2 = str.substring(j, j+1);
                    if (tempStr.equals(tempStr2)){
                        String tempStr3 = str.substring(i,j+1 );
                        if (substring == null){
                            substring = tempStr3;
                        }else{
                            if (substring.length() > tempStr3.length()){
                                substring = tempStr3;
                            }
                        }
                    }
                }
            }
            System.out.println(substring);
            scanner.close();
        }
    }
    
原文地址:https://www.cnblogs.com/zwscode/p/14284046.html