首页 > 专题系列 > Java解POJ > POJ 2624 4th Point [解题报告] Java
2013
11-11

POJ 2624 4th Point [解题报告] Java

4th Point

问题描述 :

Given are the (x,y) coordinates of the endpoints of two adjacent sides of a parallelogram. Find the (x,y) coordinates of the fourth point.

输入:

Each line of input contains eight floating point numbers: the (x,y) coordinates of one of the endpoints of the first side followed by the (x,y) coordinates of the other endpoint of the first side, followed by the (x,y) coordinates of one of the endpoints of the second side followed by the (x,y) coordinates of the other endpoint of the second side. All coordinates are in meters, to the nearest mm. All coordinates are between -10000 and +10000.

输出:

For each line of input, print the (x,y) coordinates of the fourth point of the parallelogram in meters, to the nearest mm, separated by a single space.

样例输入:

0.000 0.000 0.000 1.000 0.000 1.000 1.000 1.000
1.000 0.000 3.500 3.500 3.500 3.500 0.000 1.000
1.866 0.000 3.127 3.543 3.127 3.543 1.412 3.145

样例输出:

1.000 0.000
-2.500 -2.500
0.151 -0.398

解题代码:

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{

 public static void main(String args[])
{
 Scanner sc=new Scanner(System.in);
 
	double x1,y1,x2,y2,x3,y3,x4,y4,x5=0,y5=0;
	while(sc.hasNext())
	{
       x1=sc.nextDouble();
       y1=sc.nextDouble();
       x2=sc.nextDouble();
       y2=sc.nextDouble();
       x3=sc.nextDouble();
       y3=sc.nextDouble();
       x4=sc.nextDouble();
       y4=sc.nextDouble();
	   if(x1==x3&&y1==y3)
		{
			x5=x4-x3+x2;
			y5=y4-y3+y2;
		}
		else if(x1==x4&&y1==y4)
		{
			x5=x2-x1+x3;
			y5=y2-y1+y3;
		}
		else if(x2==x4&&y2==y4)
		{
			x5=x1-x2+x3;
			y5=y1-y2+y3;
		}
		else if(x2==x3&&y2==y3)
		{
			x5=x1-x2+x4;
			y5=y1-y2+y4;
		}
		System.out.printf("%4.3f %4.3f\n",x5,y5);
	}
 }
}

  1. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c