8241. Function 3

 

Implement the function f(x, y) = x2 + sin(x * y) – y2.

 

Input. Each line contains two real numbers x and y.

 

Output. For each test case, print on a separate line the value of the function f(x, y) with four decimal places.

 

Sample input

Sample output

2.234 2.12

10 23

56.1 0.012

23.26 5.1

-0.5034

-429.6161

3147.8333

514.3327

 

 

SOLUTION

functions

 

Algorithm analysis

Implement the funtion f(x, y). Read the input data until the end of the file.

 

Algorithm implementation

Implement the function f.

 

double f(double x, double y)

{

  return x * x + sin(x * y) - y * y;

}

 

The main part of the program. Read the input data until the end of the file.

 

while (scanf("%lf %lf", &x, &y) == 2)

  printf("%.4lf\n", f(x, y));

 

Java implementation

 

import java.util.*;

 

public class Main

{

  static double f(double x, double y)

  {

    return x * x + Math.sin(x * y) - y * y;

  }

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    while(con.hasNext())

    {

      double x = con.nextDouble();

      double y = con.nextDouble();

      System.out.printf("%.4f\n",f(x,y));

    }

    con.close();

  }

}  

 

Python implementation

 

import sys

import math

 

Implement the function f.

 

def f(x,y):

  return x * x + math.sin(x * y) - y * y

 

The main part of the program. Read the input data until the end of the file.

 

for x in sys.stdin:

  x, y =  map(float, x.split())

  print(f(x,y))