区域内点的个数java

本文介绍了一个简单的数星星算法题目,通过输入矩形区域和星星坐标来计算位于该区域内的星星数量。文章提供了完整的Java代码示例,展示了如何判断一个点是否在矩形内,并通过循环迭代完成计数。
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;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值