Problem Description
X晚上睡不着的时候不喜欢玩手机,也不喜欢打游戏,他喜欢数星星。
Input
多组输入。
每组先输入一个整数N(N <= 10000),接着输入两个点代表矩形的左下点B(x,y)和右上点T(x,y),然后输入N个(X,Y)代表N颗星星。问有多少颗星星在窗子内部,在窗边上的不计。
Output
输出一个整数,代表有多少颗星星在窗子内部。
Sample Input
3 0 1 3 4 1 1 2 2 3 3
Sample Output
1
Hint
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
int n,x,y;
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
n = sc.nextInt();
int count = 0;
xing p = new xing(sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt());
for(int i=0;i<n;i++)
{
x = sc.nextInt();
y = sc.nextInt();
int k = p.count(x, y);
if(k==1)
{
count++;
}
}
System.out.println(count);
}
}
}
class xing {
int x1,y1,x2,y2;
public xing(int x1, int y1, int x2, int y2) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public int count(int x,int y) {
if(x>x1&&x<x2 && y>y1&&y<y2)
{
return 1;
}
return 0;
}
}
本文介绍了一个简单的数星星算法题目,通过输入矩形区域和星星坐标来计算位于该区域内的星星数量。文章提供了完整的Java代码示例,展示了如何判断一个点是否在矩形内,并通过循环迭代完成计数。
601

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



