HackerRank : Week of Code 23 – Treasure Hunting

By | September 14, 2016

am is a young treasure hunter located at point and wants to get to the amazing treasure at point .

He has a weird machine that can transport him from some point to point , where is a direction vector and is a real number called a tangent coefficient. Let be the vector orthogonal to which has the same length and lies counter-clockwise from ; Sam can then also move from point to point , where is a real number called a normal coefficient.

Given , , , and , find the values of and for Sam’s journey from point to point using the weird machine. Then print the value of on a new line and the value of on the subsequent line.

Input Format

The first line contains two space-separated integers denoting the respective values of and .
The second line contains two space-separated integers denoting the respective values of and .

Constraints

Output Format

Find two real numbers, and , such that . Then print as your first line of output and as your second line of output. Your answer is considered to be correct if its absolute error is .

Sample Input

5 3
1 1

Sample Output

4.000000000000 
-1.000000000000

Solution:

Solution

import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
long x=scan.nextLong();
long y=scan.nextLong();
long a=scan.nextLong();
long b=scan.nextLong();
long aa=-b;
long bb=a;

double k=0;
double n=0;
k= (((x*bb)-(y*aa))*1.00)/((a*bb)-(b*aa));
n=(((x*b)-(y*a))*1.00)/((aa*b)-(bb*a));
System.out.println(String.format("%.12f", k));
System.out.println(String.format("%.12f", n));
}
}