蓝桥杯Java省赛模拟赛

@ 蓝桥杯 Java软件开发 省赛模拟0425

重新做一遍打发时间,之前做的直接放最后面了


#1

问题描述

不超过19000的正整数中,与19000互质的数的个数是多少?


答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。


7200

calcCode:

public class Test {

    static final int MOD = 19000;

    public static void main(String[] args) {
        int mod = MOD, cnt = MOD;
        boolean[] marked = new boolean[mod + 1];
        for (int i = 2; mod > 1; i++)
            if (mod % i == 0) {
                while (mod % i == 0) mod /= i;
                for (int k = i; k <= MOD; k += i) {
                    if (!marked[k]) cnt--;
                    marked[k] = true;
                }
            }
        System.out.print(cnt);
    }
}

求互质数个数的话,把因数和因数的倍数全ban掉就行,没啥好说的


#2

问题描述

请问十六进制数1949对应的十进制数是多少?请特别注意给定的是十六进制,求的是十进制。


答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。


6473

calcCode:

public class Test {

    public static void main(String[] args) {
        System.out.print(Integer.valueOf("1949", 16));
    }
}

这题大概就是,在计算机卷子上选择填空你当前正在考试的科目吧

大概


#3

问题描述

70044与113148的最大公约数是多少?


答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。


5388

calcCode:

public class Test {

    public static void main(String[] args) {
        System.out.print(gcd(70044, 113148));
    }

    static int gcd(int a, int b) { return b == 0? a: gcd(b, a % b); }

	static int lcm(int a, int b) { return a * b / gcd(a, b); }
}

辗转相除,8懂就背代码

反正我是一知半解,靠往DNF乱刻东西写的


#4

问题描述

一棵10层的二叉树,最多包含多少个结点?
注意当一棵二叉树只有一个结点时为一层。


答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。


1023

calcCode:

public class Test {

    public static void main(String[] args) {
        System.out.print((1 << 10) - 1);
    }
}

求最多包含节点数那就是求深度为 k k k 的满二叉树的节点个数

而满二叉树的定义为 一颗深度为 k k k ,且拥有 2 k − 1 2^k - 1 2k1 个节点的二叉树

或者硬核一点:

public class Test {

    static final int MAXDEPTH = 10;

    public static void main(String[] args) throws IOException {
        System.out.print(calc(bulid(0)));
    }

    static Node bulid(int depth) {
        if (depth == MAXDEPTH) return null;
        return new Node(bulid(depth + 1), bulid(depth + 1));
    }

    static int calc(Node node) {
        if (node == null) return 0;
        return 1 + calc(node.left) + calc(node.right);
    }

    static class Node {

        Node left, right;

        Node(Node left, Node right) {
            this.left = left;
            this.right = right;
        }
    }
}

#5

问题描述

在数列 a 1 , a 2 , … , a n a_1,a_2, …, a_n a1,a2,,an 中,定义两个元素 a i a_i ai a j a_j aj 的距离为 ∣ i − j ∣ + ∣ a i − a j ∣ |i-j|+|a_i-a_j| ij+aiaj,即元素下标的距离加上元素值的差的绝对值,其中 |x| 表示 x 的绝对值。
给定一个数列,请问找出元素之间最大的元素距离。


输入格式

输入的第一行包含一个整数 n n n
第二行包含 n n n 个整数 a 1 , a 2 , … , a n a_1, a_2, …, a_n a1,a2,,an,相邻的整数间用空格分隔,表示给定的数列。


输出格式

输出一行包含一个整数,表示答案。


测试样例1

Input:
5
9 4 2 4 7

Output:
9

Explanation:
a1 和 a3 的距离为 |1-3|+|9-2|=9。

数据规模与约定

对于 50% 的评测用例,2 <= n <= 100,0 <= 数列中的数 <= 1000。
对于所有评测用例,2 <= n <= 1000,0 <= 数列中的数 <= 10000。


code:

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in, " \n");
        int n = in.nextInt(), max = 0;
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        for (int i = n - 1; i >= 1; i--)
            for (int j = i - 1; j >= 0; j--) 
            	max = max(max, abs(i - j) + abs(a[i] - a[j]));
        System.out.print(max);
    }

    static int max(int a, int b) { return a > b? a: b; }

    static int abs(int a) { return a > 0? a: -a; }

    static class InputReader {

        BufferedReader read;
        StringTokenizer tok;
        String delimiters;

        InputReader(InputStream in) { this(in, " \n\t\r\f"); }

        InputReader(InputStream in, String delimiters) {
            this.read = new BufferedReader(new InputStreamReader(in));
            this.tok = new StringTokenizer("", this.delimiters = delimiters);
        }

        String next() {
            while (!tok.hasMoreTokens())
                try {
                    tok = new StringTokenizer(read.readLine(), delimiters);
                } catch (NullPointerException e) {
                    return null;
                }catch (IOException e) { e.fillInStackTrace(); }
            return tok.nextToken();
        }

        int nextInt() { return Integer.parseInt(next()); }
    }
}

数据规模很小,而且我也没想到什么好的优化法子,就这么暴力安排吧


#6

问题描述

小明非常不喜欢数字 2,包括那些数位上包含数字 2 的数。如果一个数的数位不包含数字 2,小明将它称为洁净数。
请问在整数 1 至 n 中,洁净数有多少个?


输入格式

输入的第一行包含一个整数 n。


输出格式

输出一行包含一个整数,表示答案。


测试样例1

Input:
30

Output:
18

数据规模与约定

对于 40% 的评测用例,1 <= n <= 10000。
对于 80% 的评测用例,1 <= n <= 100000。
对于所有评测用例,1 <= n <= 1000000。


code:

import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static void main(String[] args) throws IOException {
        int n = nextInt(System.in), cnt = 0;
        agent: for (int i = 1, k = 1; i <= n; k = ++i) {
            do if (k % 10 == 2) continue agent;
            while ((k /= 10) > 0);
            cnt++;
        }
        System.out.print(cnt);
    }

    static int nextInt(InputStream in) throws IOException {
        int n = 0, c = in.read();
        while (c < '0' || c > '9') c = in.read();
        while (c >='0' && c <='9') {
            n = n * 10 + (c & 0xf);
            c = in.read();
        }
        return n;
    }
}

还行,即使输入约定的最大值,也可以在 1s 内得到答案

但仔细一想还是能总结出 !纯净数(以下都统计到变量 noir 中) 的规律的

n 增长noir 增长
n = 10noir = 1
n = 100noir = 19
n = 1000noir = 271
n = 10000noir = 3439
n = 100000noir = 40951
n = 1000000noir = 468559

和我设想的增量一致,总结成公式
F 10 = 1 F_{10} = 1 F10=1,当 n n n 以10为倍数增长时,
F n = F n / 10 ∗ 10 + n / 10 − F n / 10 F_n = F_{n / 10} * 10 + n / 10 - F_{n / 10} Fn=Fn/1010+n/10Fn/10

然后我们写个程序和一个效验程序看下能不能炸胡

吃个饭

好吃完饭我放弃了,下一个


#7

问题描述

在数列 a[1], a[2], …, a[n] 中,如果 a[i] < a[i+1] < a[i+2] < … < a[j],则称 a[i] 至 a[j] 为一段递增序列,长度为 j-i+1。
给定一个数列,请问数列中最长的递增序列有多长。


输入格式

输入的第一行包含一个整数 n。
第二行包含 n 个整数 a[1], a[2], …, a[n],相邻的整数间用空格分隔,表示给定的数列。


输出格式

输出一行包含一个整数,表示答案。


测试样例1

Input:
7
5 2 4 1 3 7 2

Output:
3

数据规模与约定

对于 50% 的评测用例,2 <= n <= 100,0 <= 数列中的数 <= 1000。
对于所有评测用例,2 <= n <= 1000,0 <= 数列中的数 <= 10000。


code:

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in, " \n");
        int res = 0;
        for (int i = 0, n = in.nextInt(), pre = 0, cnt = 0, now; i < n; pre = now, i++) {
            now = in.nextInt();
            if (now > pre) cnt++;
            else cnt = 1;
            if (cnt > res) res = cnt;
        }
        System.out.print(res);
    }

    static class InputReader {

        BufferedReader read;
        StringTokenizer tok;
        String delimiters;

        InputReader(InputStream in) { this(in, " \n\t\r\f"); }

        InputReader(InputStream in, String delimiters) {
            this.read = new BufferedReader(new InputStreamReader(in));
            this.tok = new StringTokenizer("", this.delimiters = delimiters);
        }

        String next() {
            while (!tok.hasMoreTokens())
                try {
                    tok = new StringTokenizer(read.readLine(), delimiters);
                } catch (IOException e) { }
            return tok.nextToken();
        }

        int nextInt() { return Integer.parseInt(next()); }
    }
}

贪心,既然是连续区间的话,在它无法连续时,他就只能这样了


#8

问题描述

给定一个单词,请计算这个单词中有多少个元音字母,多少个辅音字母。
元音字母包括 a, e, i, o, u,共五个,其他均为辅音字母。


输入格式

输入一行,包含一个单词,单词中只包含小写英文字母。


输出格式

输出两行,第一行包含一个整数,表示元音字母的数量。
第二行包含一个整数,表示辅音字母的数量。


测试样例1

Input:
lanqiao

Output:
4
3

数据规模与约定

对于所有评测用例,单词中的字母个数不超过100。


code:

public class Main {

    public static void main(String[] args) throws IOException {
        int c = getByte(), a = 0, b = 0;
        while (c < 'a' || c > 'z') c = getByte();
        while (c >='a' && c <='z') {
            switch (c) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':a++; break;
                default: b++;
            }
            c = getByte();
        }
        System.out.print(a + "\n" + b);
    }

    static byte[] buff = new byte[128];
    static int len, next;
    static int getByte() {
        if (next >= len)
            try {
                next = 0;
                len = System.in.read(buff);
            } catch (IOException e) { }
        return buff[next++];
    }
}

都挺简单的,可能是在考Java基础吧


#9

问题描述

小明每天都要练功,练功中的重要一项是梅花桩。
小明练功的梅花桩排列成 n 行 m 列,相邻两行的距离为 1,相邻两列的距离也为 1。
小明站在第 1 行第 1 列上,他要走到第 n 行第 m 列上。小明已经练了一段时间,他现在可以一步移动不超过 d 的距离(直线距离)。
小明想知道,在不掉下梅花桩的情况下,自己最少要多少步可以移动到目标。


输入格式

输入的第一行包含两个整数 n, m,分别表示梅花桩的行数和列数。
第二行包含一个实数 d(最多包含一位小数),表示小明一步可以移动的距离。


输出格式

输出一个整数,表示小明最少多少步可以到达目标。


测试样例1

Input:
3 4
1.5

Output:
3

数据规模与约定

对于 30% 的评测用例,2 <= n, m <= 20,1 <= d <= 20。
对于 60% 的评测用例,2 <= n, m <= 100,1 <= d <= 100。
对于所有评测用例,2 <= n, m <= 1000,1 <= d <= 100。


code:

import java.io.IOException;
import java.io.BufferedReader;
import java.io.StreamTokenizer;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int)in.nval;
        in.nextToken();
        int m = (int)in.nval;
        in.nextToken();
        double d = in.nval, win = d * d;
        Queue<Step> queue = new LinkedList();
        queue.offer(new Step(1, 1, 0));
        while (queue.size() > 0) {
            Step now = queue.poll();
            if (now.x >= n && now.y >= m) {
                System.out.print(now.step);
                return;
            }
            int x = now.x;
            int y = now.y + (int)d;
            if (y > m) y = m;
            while (y >= now.y && x <= n) {
                if (sque(now.x - x) + sque(now.y - y) <= win) {
                    queue.offer(new Step(x, y, now.step + 1));
                    x++;
                } else y--;
            }
        }
    }

    static int sque(int a) { return a * a; }

    static class Step {

        int x, y, step;

        Step(int x, int y, int step) {
            this.x = x;
            this.y = y;
            this.step = step;
        }
    }
}

广搜,确保下一步都会走到最右下

因为可能会出现极端长方形的用例,就是很长很长的长方形,若不限制越界问题,2 x 1000 的长方形就跟你当 1000 x 1000 的正方形处理了

但这么写也只能针对 x < y 的情况,因为可能存在弯道超车的可能性

在这里插入图片描述
所以如果成立的话,就不能触底直接输出了

针对我能想到的情况,可以这么写:

int x = now.x, y = now.y;
if (y == m) {
    x += d;
	if (x > n) x = n;
} else {
	y += d;
	if (y > m) y = m;
}
while (y >= now.y) { . . . }

没有那种缜密的建模能力,又没有完备的测试用例
但问题不大

就这样吧


#10

问题描述

小明用积木搭了一个城堡。
为了方便,小明在搭的时候用的是一样大小的正方体积本,搭在了一个 n 行 m 列的方格图上,每个积木正好占据方格图的一个小方格。
当然,小明的城堡并不是平面的,而是立体的。小明可以将积木垒在别的积木上面。当一个方格上的积木垒得比较高时,就是一个高塔,当一个方格上没有积木时,就是一块平地。
小明的城堡可以用每个方格上垒的积木层数来表示。例如,下面就表示一个城堡。
  9 3 3 1
  3 3 3 0
  0 0 0 0
这个城堡南面和东面都有空地,西北面有一个大房子,在西北角还有一个高塔,东北角有一个车库。
现在,格格巫要来破坏小明的城堡,他施了魔法水淹小明的城堡。
如果水的高度为1,则紧贴地面的那些积木要被水淹,在上面的例子中,有7块积木要被水淹。
如果水的高度为2,则更多积木要被水淹,在上面的例子中,有13块积木要被水淹。
给定小明的城堡图,请问,水的高度依次为1, 2, 3, …, H 时,有多少块积木要被水淹。


输入格式

输入的第一行包含两个整数 n, m。
接下来 n 行,每行 m 个整数,表示小明的城堡中每个位置积木的层数。
接下来包含一个整数 H,表示水高度的上限。


输出格式

输出 H 行,每行一个整数。第 i 的整数表示水的高度为 i 时被水淹的积木数量。


测试样例1

Input:
3 4
9 3 3 1
3 3 3 0
0 0 0 0
10

Output:
7
13
19
20
21
22
23
24
25
25

数据规模与约定

对于 40% 的评测用例,1 <= n, m <= 100,1 <= H <= 100,积木层数不超过100;
对于 70% 的评测用例,1 <= n, m <= 1000,1 <= H <= 1000,积木层数不超过1000;
对于所有评测用例,1 <= n, m <= 1000,1 <= H <= 100000,积木层数不超过1000000000。。


code:

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in, " \n");
        int n = in.nextInt(), m = in.nextInt();
        int[][] map = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                map[i][j] = in.nextInt();
        int h = in.nextInt();
        for (int cnt = 0; h > 0; h--) {
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++) if (--map[i][j] >= 0) cnt++;
            System.out.println(cnt);
        }
    }

    static class InputReader {

        BufferedReader read;
        StringTokenizer tok;
        String delimiters;

        InputReader(InputStream in) { this(in, " \n\t\r\f"); }

        InputReader(InputStream in, String delimiters) {
            this.read = new BufferedReader(new InputStreamReader(in));
            this.tok = new StringTokenizer("", this.delimiters = delimiters);
        }

        String next() {
            while (!tok.hasMoreTokens())
                try {
                    tok = new StringTokenizer(read.readLine(), delimiters);
                } catch (IOException e) { }
            return tok.nextToken();
        }

        int nextInt() { return Integer.parseInt(next()); }
    }
}

可能是这样,也可能是不是,关键在于如何定义被水淹

明明可以给出一个更大的测试用例给选手逆运算,但偏偏要这样故弄玄虚

随意优化一下就这么过了吧

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in, " \n");
        PrintWriter out = new PrintWriter(System.out);
        int n = in.nextInt(), m = in.nextInt(), v, h;
        int[] map = new int[v = n * m];
        for (int i = 0; i < v; i++)
            map[i] = in.nextInt();
        Arrays.sort(map, 0, v);
        h = in.nextInt();
        for (int k = 1, cnt = 0; k <= h; k++) {
            for (int i = v - 1; i >= 0; i--) {
                if (map[i] < k) break;
                cnt++;
            }
            out.println(cnt);
        }
        out.close();
    }

    static class InputReader {

        BufferedReader read;
        StringTokenizer tok;
        String delimiters;

        InputReader(InputStream in) { this(in, " \n\t\r\f"); }

        InputReader(InputStream in, String delimiters) {
            this.read = new BufferedReader(new InputStreamReader(in));
            this.tok = new StringTokenizer("", this.delimiters = delimiters);
        }

        String next() {
            while (!tok.hasMoreTokens())
                try {
                    tok = new StringTokenizer(read.readLine(), delimiters);
                } catch (IOException e) { }
            return tok.nextToken();
        }

        int nextInt() { return Integer.parseInt(next()); }
    }
}

完事,收工


#1

import java.util.ArrayList;
import java.util.List;

public class Test {

    static final int mod = 19000;

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 2; i <= mod; i++)
            if (mod % i == 0)
                for (int j = i; j <= mod; j += i)
                    if (list.contains(j)) continue;
                    else list.add(j);
        System.out.println(mod - list.size());
    }
}

#2

public class Test {

    public static void main(String[] args) {
        System.out.println(Integer.valueOf("1949",0x10));
    }
}

#3

public class Test {

    static final int mod1 = 70044;
    static final int mod2 = 113148;
    static int flag = 0;

    public static void main(String[] args) {
        for (int i = 1, j = mod2 / 2; i <= j; i++)
            if (mod1 % i == 0 && mod2 % i == 0) flag = i;
        System.out.println(flag);
    }
}

#4

public class Test {
    
    public static void main(String[] args) {
        System.out.println((int)Math.pow(2, 10) - 1);
    }
}

#5

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), flag = 0;
        int[] line = new int[n];
        for (int i = 0; i < n; i++) line[i] = sc.nextInt();
        for (int i = n - 1; i > 0; i--)
            for (int j = i - 1; j >= 0; j--)
                flag = Math.max(flag, Math.abs(i - j) + Math.abs(line[i] - line[j]));
        System.out.println(flag);
    }
}

#6

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), num = 0;
        flag: for (int i = 1, j = i; i <= n; j = ++i) {
            do if (j % 10 == 2) continue flag;
            while ((j /= 10) != 0);
            num++;
        }
        System.out.println(num);
    }
}

#7

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), flag = 1, start = 0, end = flag, mid;;
        int[] line = new int[n],
            dp = new int[n + 1];
        for (int i = 0; i < n; i++) line[i] = sc.nextInt();
        dp[1] = line[0];
        for(int i = 1; i < n; i++){
            if(line[i] > dp[flag]) dp[++flag] = line[i];
            else{
                start = 1;end = flag;
                while(start <= end)
                    if(dp[(mid = (start + end) / 2)] < line[i]) start = mid + 1;
                    else end = mid - 1;
                dp[start] = line[i];
            }
        }
        System.out.println(flag);
    }
}

#8

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int Vowel = 0, noVowel = 0;
        char[] words = sc.next().toCharArray();
        for (int i = words.length - 1; i >=0; i--)
            switch (words[i]) {
                case 'a': Vowel++; break;
                case 'e': Vowel++; break;
                case 'i': Vowel++; break;
                case 'o': Vowel++; break;
                case 'u': Vowel++; break;
                default: noVowel++; break;
            }
        System.out.print(Vowel + "\n" + noVowel);
    }
}

#9

import java.util.Scanner;

public class Main {

    static int n, m;
    static double d, win;
    static int[][] dp;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt(); m = sc.nextInt();
        d = sc.nextDouble(); win = d * d;
        dp = new int[n + 1][m + 1];
        Lost(1, 1);
        System.out.println(dp[n][m]);
    }
    static void Lost(int x, int y) {
        int x1 = x, y1 = y + (int)d;
        if (Math.pow(n - x, 2) + Math.pow(m - y, 2) <= win) {
            dp[n][m] = dp[x][y] + 1;
            return;
        }
        if (y == m) dp[n][m] = dp[x][y] + (int)Math.ceil((n - x) / (int)d);
        while (x1 <= n && y1 >= y && y1 <= m){
            if(Math.pow(x1 - x, 2) + Math.pow(y1 - y, 2) <= win) {
                dp[x1][y1] = dp[x][y] + 1;
                Lost(x1, y1);
                x1++;
            } else y1--;
        }
    }
}

#10

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt(), H, l = n * m;
        Long[] map = new Long[l--];
        for (int i = 0; i <= l; i++) map[i] = sc.nextLong();
        H = sc.nextInt();
        Arrays.sort(map);
        int[] record = new int[H + 1];
        for (int i = 1; i <= H; i++)
            for (int j = l; j >=0; j--)
                if(map[j]==0) break;
                else if (map[j] <= i) record[i] += map[j];
                else record[i] += i;
        for (int i = 1; i <= H; i++)
            System.out.println(record[i]);
    }
}

吐槽全给我删了,也罢

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值