首页 > 专题系列 > Java解POJ > POJ 1254 Hansel and Grethel [解题报告] Java
2013
11-09

POJ 1254 Hansel and Grethel [解题报告] Java

Hansel and Grethel

问题描述 :

On a warm summer afternoon, Hansel and Grethel are walking together in the fields. It is getting late and, to be honest, they are lost. Grethel is a little scared, still vividly remembering the last time they got lost in the forest. That time, an evil witch had locked them inside a house built of gingerbread and sugar! But Hansel can reassure her: this time they are well prepared. Hansel has taken a map and a compass with him!

Hansel picks two clearly outstanding features in the landscape, and uses the compass to measure the direction towards both objects. Grethel locates the objects on the map, and writes down the corresponding map coordinates. Based on this information, they will be able to accurately determine their own position on the map.



The coordinates of two marker objects, and the direction (angle from the North) towards these objects are known. Write a program which uses this data to calculate the coordinates of Hansel and Grethel’s current location.

输入:

The first line of the input contains one positive number: the number of situations in which a position must be determined. Following are two lines per situation, describing the two marker objects. Each marker object is described by a line containing three integer numbers:

 the x-coordinate of the object on the map (0 <= x <= 100);

the x-axis runs West-to-East on the map, with increasing values towards the East.

 the y-coordinate of the object on the map (0 <= y <= 100);

the y-axis runs South-to-North on the map, with increasing values towards the North.

 the direction d of the object in degrees (0 <= d <= 360);

with 0 degree = North, 90 degree = East, 180 degree = South, and so on.

To keep the position calculations accurate, Hansel makes sure that the directions of the two

objects are not exactly equal, and do not differ by exactly 180 degree.

输出:

One line per situation, containing the result of the position calculation: two numbers, separated by a space, each having exactly 4 digits after the decimal point. These numbers represent the x and y coordinates of the position of Hansel and Grethel (0 <= x,y <= 100). Round the numbers as usual: up if the next digit would be >= 5, down otherwise.

样例输入:

2
30 50 90
20 40 180
30 40 96
20 20 150

样例输出:

20.0000 50.0000
7.0610 42.4110

解题代码:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main{

public static void main(String[] args) {
   Scanner cin = new Scanner(System.in);
   DecimalFormat df1 = new DecimalFormat("0.0000"); 
   int n = cin.nextInt();
   while(n-- > 0) {
    int x1 = cin.nextInt();
    int y1 = cin.nextInt();
    double t1 = cin.nextInt();
    int x2 = cin.nextInt();
    int y2 = cin.nextInt();
    double t2 = cin.nextInt();
    double x, y;
    t1=t1 / 360 * 2 * Math.PI;
    t2=t2 / 360 * 2 * Math.PI;
      y = (Math.cos(t2)*(Math.sin(t1)*y1-Math.cos(t1)*x1)-Math.cos(t1)*(Math.sin(t2)*y2-Math.cos(t2)*x2))/(Math.sin(t1-t2));
    x = (Math.sin(t2)*(Math.sin(t1)*y1-Math.cos(t1)*x1)-Math.sin(t1)*(Math.sin(t2)*y2-Math.cos(t2)*x2))/(Math.sin(t1-t2));
    System.out.println(df1.format(x)+" "+ df1.format(y));
   }
  
}

}