题目
输入
输出
输出一个整数,表示输入灰度图像中处于较暗区域的像素总数。
输入样例1
4 16 1 6
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
输出样例1
7
输入样例2
11 8 2 2
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 7 0 0 0 7 0 0 7 7 0
7 0 7 0 7 0 7 0 7 0 7
7 0 0 0 7 0 0 0 7 0 7
7 0 0 0 0 7 0 0 7 7 0
7 0 0 0 0 0 7 0 7 0 0
7 0 7 0 7 0 7 0 7 0 0
0 7 0 0 0 7 0 0 7 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
输出样例2
83
题解
思路:一维前缀和,有兴趣可以使用二维前缀和
package package2021_11;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int l=sc.nextInt();
int r=sc.nextInt();
int t=sc.nextInt();
int count=0;//表示结果
int[][] nums=new int[n][n+1];
//获得前缀和
for (int i = 0; i < n; i++) {
for (int j = 0; j <n; j++) {
int a=sc.nextInt();
nums[i][j+1]=nums[i][j]+a;
}
}
//数据处理
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int res=0;
//获取前后坐标
int row1=(i-r)>=0?(i-r):0;
int col1=(j-r)>=0?(j-r):0;
int row2=(i+r)<n?(i+r):n-1;
int col2=(j+r)<n?(j+r):n-1;
//表示上面方框的数量大小
int num=(col2-col1+1)*(row2-row1+1);
for (int k = row1; k <=row2; k++) {
res+=(nums[k][col2+1]-nums[k][col1]);
}
if(res<=num*t)
count++;
}
}
System.out.println(count);
}
}