题目:



思路:
1、创建1001*1001大小的数组,默认值为0;
2、依次输入星星坐标,根据坐标,将数组中对应元素的值设为1;
3、遍历数组,通过累加的方式,算出对应坐标到(1,1)点内矩阵中星星的个数;

4、求指定区域内星星的个数。
程序:
import java.util.Scanner;
public class subject2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] array = new int[1001][1001];//创建1001*1001大小的数组
for(int i = 0; i < n; i ++) {
array[sc.nextInt()][sc.nextInt()] = 1;//将数组中有星星的地方,元素值设为1
}
//累加array,array[i][j]表示(i,j)和(1,1)构成的矩形阵列中的星星总数
for(int i = 1; i < array.length; i ++) {
for(int j = 1; j < array[0].length; j ++) {
array[i][j] += array[i-1][j] + array[i][j-1] - array[i-1][j-1];
}
}
System.out.println();
int count = 0;
int m = sc.nextInt();
for(int i = 0; i < m; i ++) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
count = array[x2][y2] - array[x2][y1-1] - array[x1-1][y2] + array[x1-1][x2-1];
System.out.println(count);
}
sc.close();
}
}
该博客主要围绕Java程序展开,介绍了统计星星个数的思路。先创建1001*1001大小的数组,输入星星坐标并将对应元素设为1,接着遍历数组累加算出到(1,1)点内矩阵中星星个数,最后求指定区域内星星个数。
860

被折叠的 条评论
为什么被折叠?



