CCF计算机软件能力认证202104前两题java满分解

这篇文章展示了两个Java程序片段,分别涉及如何使用二维数组计算前缀和以及在给定范围内统计满足特定条件的元素数量。第一个程序计算数组中每个元素出现的次数,第二个程序则计算一个二维数组中元素值与其周围元素之和的比值小于给定阈值的元素数量。
摘要由CSDN通过智能技术生成

第一题

package ccf202104;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class p1 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] temp = bf.readLine().split(" ");
        int n = Integer.parseInt(temp[0]);
        int m = Integer.parseInt(temp[1]);
        int L = Integer.parseInt(temp[2]);
        int[] h = new int[L];
        int[][] a = new int[n][m];
        for (int i = 0; i < n; i++) {
            temp = bf.readLine().split(" ");
            for (int j = 0; j < temp.length; j++) {
                a[i][j] = Integer.parseInt(temp[j]);
                h[a[i][j]]++;
            }
        }
        for (int i = 0; i < L; i++) {
            System.out.print(h[i] + " ");
        }
    }
}

第二题(使用了二维前缀和,邻域不只是包括该元素周围的元素,也包括该元素自身)

package ccf202104;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class p2 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] temp = bf.readLine().split(" ");
        int n = Integer.parseInt(temp[0]);
        int L = Integer.parseInt(temp[1]);
        int r = Integer.parseInt(temp[2]);
        int t = Integer.parseInt(temp[3]);
        int[][] a = new int[n + 1][n + 1];
        int[][] sum = new int[n + 1][n + 1];
        double[][][] nei;
        for (int i = 1; i <= n; i++) {
            temp = bf.readLine().split(" ");
            int index = 0;
            for (int j = 1; j <= n; j++) {
                a[i][j] = Integer.parseInt(temp[index]);
                index++;
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
            }
        }
        nei = getNei(sum, a, n, r);
        int count = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                double compare = nei[i][j][0] / nei[i][j][1];
                if (compare <= t) {
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    public static double[][][] getNei(int[][] sum, int[][] a, int n, int r) {
        double[][][] nei = new double[n + 1][n + 1][2];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                int x1 = i - r;
                int y1 = j - r;
                int x2 = i + r;
                int y2 = j + r;
                if (x1 < 1) {
                    x1 = 1;
                }
                if (y1 < 1) {
                    y1 = 1;
                }
                if (x2 > n) {
                    x2 = n;
                }
                if (y2 > n) {
                    y2 = n;
                }
                nei[i][j][0] = sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
                nei[i][j][1] = (x2 - (x1 - 1)) * (y2 - (y1 - 1)) ;
            }
        }
        return nei;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值