第二届全国高校计算机能力挑战赛-Java程序设计赛

第二届全国高校计算机能力挑战赛-Java程序设计赛

2020年第二届全国高校计算机能力挑战赛-Java程序设计赛前15题为选择题。16,17,18,19为编程题。

16题
题目:统计从1到N的整数中,所有立方值的平方根为整数的数的个数
输入说明:整数N(N<10000);
输出说明:符合条件的数的个数,如43=64=82
输入样例:10
输出样例:3
(说明:样例中符合条件的3个数是1、4、9)

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int res = 0;
        for ( int i = 1; i <= n; i++){
            long li = i*i*i;
            long s = (int) Math.sqrt(li);
            if ( s * s == li )
                res++;
        }
        System.out.println(res);
        sc.close();
    }
}

17题
题目:数组a和b分别记录着球队A和B本赛季N场比赛的净胜球,其中正数为胜利积3分,负数为失败积0分,0为平局积1份,求解A和B的排名先后。如果积分相同则净胜球总数多则排名靠前。如果净胜球总数仍相同,则输出: Draw。
输入说明:第一行,数组中元素个数N(N<1000);第二行,A的净胜球;第三行,B的净胜球
输出说明:排名靠前球队(A或B或Draw)
输入样例:5
1 0 -1 0 1
0 6 0 0 -1
输出样例:A

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count_a = 0, count_b = 0, score_a = 0, score_b = 0;
        for ( int i = 0; i < n; i++ ) {
            int a = sc.nextInt();
            if ( a > 0 ) {
                score_a += 3;
                count_a += a;
            } else if ( a == 0 )
                score_a += 1;
            else
                count_a += a;
        }
        for ( int i = 0; i < n; i++ ) {
            int b = sc.nextInt();
            if ( b > 0 ) {
                score_b += 3;
                count_b += b;
            } else if ( b == 0 )
                score_b += 1;
            else
                count_b += b;
        }

        if ( score_a > score_b )
            System.out.println("A");
        else if ( score_a < score_b )
            System.out.println("B");
        else {
            if( count_a > count_b )
                System.out.println("A");
            else if( count_a < count_b )
                System.out.println("B");
            else
                System.out.println("Draw");
        }
        sc.close();
    }
}

18题
题目:在一个小写英文字母(a-z)组成的字符串的最短子串,其包含这个字符串中出现过的所有字母。输出最左边的该类子串。
输入说明:待处理字串(长度≤200)
输出说明:子串
输入样例:adfasjdoiasdfa
输出样例:fasjdoi

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

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();

        HashSet<Character> hashSet = new HashSet<>();
        for(int i = 0;i < str.length();i++)
            hashSet.add(str.charAt(i));
        int len = hashSet.size();

        String res = str;
        HashSet<Character> hashSet_sub = new HashSet<>();
        for(int i = 0;i < str.length()-len+1;i++){
            String sub = str.substring(i, i+len);
            for(int j = 0;j < sub.length();j++)
                hashSet_sub.add(sub.charAt(j));

            if(hashSet.equals(hashSet_sub)){
                res = sub;
                break;
            }
            hashSet_sub.clear();
        }

        System.out.println(res);
        sc.close();
    }
}

19题
题目:某商品有2种不同数量的包装,对应不同的价格;同时提供满200元减50元的不限量购物券,试求解最佳购买策略,在单次购买中以最低总价购买正好500个商品。
输入说明:两种包装的数量和价格(均为整数)
输出说明:两种商品各自购买数量(无解则输出:-1)
输入样例:100 80 200 150
输出样例:5 0

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int one_count = sc.nextInt();
        int one_price = sc.nextInt();
        int two_count = sc.nextInt();
        int two_price = sc.nextInt();
        int min = 10000, one_res = 0, two_res = 0;
        boolean flag = false;   // 是否有解
        for ( int i = 0; i <= 500/one_count; i++ ) {
            if ( (500-i*one_count) % two_count != 0 )    // 凑不成 500
                continue;
            int temp = i * one_price + ( 500 - i * one_count ) / two_count * two_price;
            temp = temp - (temp / 200 * 50);      // 不限量 满200减50
            if ( temp < min ) {
                min = temp;
                one_res = i;
                two_res = ( 500 - one_res * one_count ) / two_count;
                flag = true;
            }
        }
        System.out.println( flag ? String.format("%d %d", one_res, two_res) : -1);
        sc.close();
    }
}
posted @ 2020-12-09 23:33  狡猾的狐狸科  阅读(518)  评论(0编辑  收藏  举报