ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

全国高校计算机能力挑战赛模拟题

2021-12-12 13:30:01  阅读:176  来源: 互联网

标签:Scanner int 高校 System 模拟题 ++ sc 挑战赛 String


全国高校计算机能力挑战赛模拟题

1.统计1到N(含)之间所有平方数的个数,并输出这个数目。
提示:平方数的个数,如4是2的平方数,16是4的平方数,5不是平方数。
输入说明:一个整数N(N<100000);
输出说明:平方数的个数
输入样例:50
输出样例:7

import java.util.Scanner;

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

2.对于给出的长度为N(N<1000)的正整数数组,满足连续3个元素均为合数的区间称为3合数区间,计算该数组中3合数区间的个数。
输入说明:第一行,数组中元素个数N,第二行,N个正整数,用空格隔开。
输出说明:3合数区间的个数
输入样例:7

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int array[] = new int[m];
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            array[i] = sc.nextInt();
        }
        sc.close();
        for (int i = 0; i < array.length - 2; i++) {
           if(is(array[i]) && is(array[i+1]) && is(array[i+2])){
               count++;
           }
        }
        System.out.println(count);
    }
    static boolean is(int number){
        for (int i = 2; i < number ; i++) {
            if(number % i == 0){
                return true;
            }
        }
        return false;
    }
}

3.字母连连看,给定一个由小写英文字母组成的字符串(长度<1000),如果字符串中有两个连续的字母相同,则这两个字母可同时消除,并不断重复该操作,直到不能消除为止。请编程判断该字符串是否可以完全消除。
输入说明:一个字符串。
输出说明:如果可以完全消除,输出“YES”,如果不可以,输出消除后的结果。
输入样例1:abacddcaba
输出样例1:YES
输入样例2:asdfghhgf
输出样例2:asd

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String string = sc.nextLine();
        sc.close();
        String result = remove(string);
        if(result.length() == 0){
            System.out.println("YES");
        }else{
            System.out.println(result);
        }
    }
    public static String remove(String string){
        String s = string;
        for (int i = 0; i < string.length() - 1; i++) {
            if(string.charAt(i) == string.charAt(i + 1)){
                s = s.substring(0,i) + s.substring(i+2,string.length());
            }
        }
        if(s.equals(string)){
            return string;
        }else {
            return remove(s);
        }
    }
}

4.由N(N<=10000)个整数组成的数组,其中连续K(K<=200)个元素构成一个区间,称为K区间。一个K区间中所有素数的和记为Sk,请计算整个数组中,所有K区间中的最大Sk值,并输出。
输入说明:第一行是两个整数N和K,第二行输入N个数,表示数组中的元素。
输出说明:最大Sk值
输入样例:8 2
12 23 27 34 19 17 45 8
输出样例:36

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        int array[] = new int[N];
        for (int i = 0; i < array.length; i++) {
            array[i] = sc.nextInt();
        }
        sc.close();
        int max = 0;
        for (int i = 0; i < N - K; i++) {
            int temp = 0;
            for (int j = i; j < i + K; j++) {
                if(is(array[j])){
                    temp = temp + array[j];
                    max = Math.max(max,temp);
                }
            }
        }
        System.out.println(max);;
    }
    public static boolean is(int number){
        for (int i = 2; i < number; i++) {
            if(number % i == 0){
                return false;
            }
        }
        return true;
    }
}

5.仓库新进了几批物资,只知道每批物资的数量和单价,请编写程序,按照每种物资的总价值,由高到低次序输出。
输入说明:第1行 一个整数N,表明物资的批次数量
第2-N+1行,每批物资的类别、数量及单价,中间用空格隔开,其中类别用A-Z加以区分。
输出说明:按物资价值降序输出排序结果,每行输出一种物资。
输入样例:5
A 5 10.00
B 3 2.00
A 5 8.00
B 3 2.50
C 10 3.50
输出样例:A 90.00
C 35.00
B 13.50

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Object [][] array = new Object[5][3];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                array[i][j] = sc.next();
            }
        }
        sc.close();
        Map<String,Double> map = new HashMap<>();
        for (int i = 0; i < N; i++) {
            double sum = Integer.parseInt((String) array[i][1]) * Double.parseDouble((String)array[i][2]);
            String key =  (String) array[i][0];
            if(map.get(key) == null){
                map.put(key,sum);
            }else{
                double temp = map.get(key);
                map.put(key,temp + sum);
            }
        }
        System.out.println(map.get("A"));
        List<Map.Entry<String, Double>> infoIds = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
        //根据value 降序排序
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
                return (int) (o2.getValue() - o1.getValue());
            }
        });
        //注意这里遍历的是list,也就是我们将map.Entry放进了list,排序后的集合
        for (Map.Entry s : infoIds)
        {
            System.out.println(s.getKey() + " " + String.format("%.2f",s.getValue()));
        }

    }

}

6.统计1到N(含)之间所有立方数的个数,并输出这个数目。
提示:立方数的个数,如8是2的立方数,27是3的立方数,9不是立方数。
输入说明:一个整数N(N<100000);
输出说明:立方数的个数
输入样例:200
输出样例:5

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.close();
        int count = 0;
        for (int i = 1; i <= N ; i++) {
            for (int j = 1; j <= i ; j++) {
                if(j * j * j == i){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

7.统计整数区间[N, M] (N,M<100000)中所有非偶数的合数个数,并输出这个数。
输入说明:两个整数N、M;
输出说明:非偶数的合数个数
输入样例:2 16
输出样例:2

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        sc.close();
        int count = 0;
        for (int i = N; i <= M ; i++) {
            if(i % 2 != 0){
                for(int j = 2;j < i;j++){
                    if(i % j == 0 ){
                        count++;
                        break;
                    }
                }
            }

        }
        System.out.println(count);
    }

}

8.对于给定的字符数组(字符数少于10000),统计其中字母类型、数字类型和符号类型的字符出现次数,其中字母类型是英文字母a-z之间的字符(不区分大小写);数字类型是0-9之间的字符;符号类型是除英文字母、数字及空格外的其它字符。
输入说明:一个字符序列;
输出说明:分三行输出:第一行字母类型,以a-z标识;第二行数字类型,以0-9标识;第三行符号类型,以others标识。每行格式如下:
类别标识 出现次数(中间用一个空格分隔)
输入样例:Hello World!
输出样例:a-z 10
0-9 0
others 1

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[]chars = s.toCharArray();
        sc.close();
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        for (int i = 0; i < chars.length; i++) {
            int temp = chars[i];
            if (temp == 32){
                continue;
            } else if(temp >= 65 && temp <= 122){
                count1++;
                continue;
            }else if(temp >= 48 && temp <= 57){
                count2++;
                continue;
            }else {
                count3++;
            }
        }

        System.out.println("a-z" + " " + count1);
        System.out.println("0-9" + " " + count2);
        System.out.println("others " + " " + count3);
    }

}

9.由N(N<=10000)个整数组成的数组,其中连续K(K<=200)个元素构成一个区间,称为K区间。一个K区间中任意两个数求其差值的绝对值,其中最大的绝对值记为Dk。请计算整个数组中,所有K区间中的最大Dk值,并输出。
输入说明:第一行是两个整数N和K,第二行输入N个数,表示数组中的元素。
输出说明:最大Dk值。
输入样例:8 2
12 23 27 34 35 36 8 45
输出样例:37

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        int array [] = new int [N];
        for (int i = 0; i < N; i++) {
            array[i] = sc.nextInt();
        }
        sc.close();
        int max = 0;
        for (int i = 0; i < N - K ; i++) {
            for (int j = i; j < i + K; j++) {
                max = Math.abs((array[j] - array[j+1]));
            }
        }
        System.out.println(max);
    }

}

10.给定一个只包含0-9、‘+’、‘’的合法数学表达式(长度<1000),规定加号‘+’的优先级高于乘号‘’,请输出计算结果。
输入说明: 合法的数学表达式
输出说明: 输出表达式的计算结果
输入样例: 123+122
输出样例: 360

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String string = sc.nextLine();
        sc.close();
        String [] multiplication = string.split("\\*");
        int result = 1;
        for (int i = 0; i < multiplication.length; i++) {
            String[] add = multiplication[i].split("\\+");
            int count = 0;
            for (int j = 0; j < add.length; j++) {
                count += Integer.parseInt(add[j]);
            }
            result *= count;
        }
        System.out.println(result);
    }

}

标签:Scanner,int,高校,System,模拟题,++,sc,挑战赛,String
来源: https://blog.csdn.net/lovewangyihui/article/details/121886038

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有