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++;
        }
    }
}

算法优化:

public static void main(String[] args) {
      int[] array = {1,4,2,5,3,19,8,6};
      int m = 10;
      int c;
      Set<Integer> set = new HashSet<>();
      for (int i = 0; i < array.length; i++) {
          c = m - array[i];
          set.add(c);
      }
      int count = 0;
      for (int i = 0; i < array.length; i++) {
          if (set.contains(array[i])){
              count++;
          }
      }
      System.out.println(count/2);
}

题目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();
        }
    }
    

该题的另一种解法,使用双指针,更直观:

public class Main07 {
    public static void main(String[] args) {
        String str = "adfdasjdoiasldhlfakjkjkjgjkdfkakdljigghuiddnickllllleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
        char[] chars = str.toCharArray();
        int i = 0,j = chars.length-1;
        int space = 999999999;
        int begin =0,end=0;
        while(i != chars.length-1){
            if (j == i){
                i++;
                j = chars.length-1;
                continue;
            }
            if (chars[i] == chars[j]){
                if (j-i<space) {
                    space = j - i;
                    begin = i;
                    end = j;
                }
            }
            j--;
        }
        for (int k = begin;k<=end;k++){
            System.out.print(chars[k]);
        }
    }
}
  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

索半斤_suobanjin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值