poj 3335 java

本文详细介绍了使用Java编程语言解决POJ 3335算法问题的步骤,包括输入处理、数据结构初始化、算法实现以及输出结果判断。重点在于通过数学方法求解线性方程组,确定是否存在交点,并输出相应的答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Scanner;

public class POJ_3335 {

	static double x[], y[];
	static double tx[], ty[];
	static double txp[], typ[];
	static int num;
	static int tnum;
	static double a, b, c;

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		for(int test=0;test<t;test++){

			int n = scan.nextInt();
			num = n;

			x = new double[105];
			y = new double[105];
			tx = new double[105];
			ty = new double[105];
			txp = new double[105];
			typ = new double[105];

			for (int i = 0; i < n; i++) {
				x[i] = scan.nextDouble();
				y[i] = scan.nextDouble();
				tx[i + 1] = x[i];
				ty[i + 1] = y[i];
			}

			x[n] = x[0];
			y[n] = y[0];
			tx[0] = tx[n];
			ty[0] = ty[n];
			tx[n + 1] = tx[1];
			ty[n + 1] = ty[1];

			for (int i = 0; i < n; i++) {
				a = y[i + 1] - y[i];      // 求  aX+bY+c = 0;的a,b,c
				b = x[i] - x[i + 1];
				c = x[i + 1] * y[i] - x[i] * y[i + 1];
				solve();
			}

			if (num == 0)
				System.out.println("NO");
			else
				System.out.println("YES");

		}

	}

	public static void solve() {

		tnum = 0;

		for (int i = 1; i <= num; i++) {

			if (sig(a * tx[i] + b * ty[i] + c) >= 0) {  //在一侧
				txp[tnum] = tx[i];
				typ[tnum++] = ty[i];
			} else {                                     // 在别一侧
				if (sig(a * tx[i - 1] + b * ty[i - 1] + c) > 0)   // 大于0才会有交点
					insert(tx[i - 1], ty[i - 1], tx[i], ty[i]);
				if (sig(a * tx[i + 1] + b * ty[i + 1] + c ) > 0)
					insert(tx[i + 1], ty[i + 1], tx[i], ty[i]);
			}
		}

		num = tnum;              // 更新tx,ty,num

		for (int j = 1; j <= num; j++) {
			tx[j] = txp[j - 1];
			ty[j] = typ[j - 1];
		}
		tx[0] = tx[num];
		ty[0] = ty[num];
		tx[num + 1] = tx[1];
		ty[num + 1] = ty[1];

	}

	private static int sig(double d) {
		if(d<-(1e-10))
			return -1;
		else if(d>1e-10)
			return 1;
		return 0;
	}

	public static void insert(double x1, double y1, double x2, double y2) {

		double xx = Math.abs(a * x1 + b * y1 + c);     //  求两直线交点
		double yy = Math.abs(a * x2 + b * y2 + c);
		txp[tnum] = (x1 * yy + x2 * xx) / (xx + yy);
		typ[tnum++] = (y1 * yy + y2 * xx) / (xx + yy);

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值