Soluție HackerRank pentru Sherlock and Moving Tiles, subdomeniul Fundamentals, în C++14. Include cerința formatată, exemple, explicația pașilor și cod…

  • Problemă: Sherlock and Moving Tiles
  • Domeniu: Fundamentals
  • Limbaj: C++14

Challenge: Sherlock and Moving Tiles

Subdomeniu: Fundamentals (fundamentals)

Scor cont: 20.0 / 20

Submission status: Accepted

Submission score: 1.0

Submission ID: 464722614

Limbaj: cpp14

Link challenge: https://www.hackerrank.com/challenges/sherlock-and-moving-tiles/problem

Cerință

Sherlock is given 2 square tiles, initially both of whose sides have length l placed in an x-y plane. Initially, the bottom left corners of each square are at the origin and their sides are parallel to the axes.

At t=0, both squares start moving along line y=x (along the positive x and y) with velocities s1 and s2.

For each querydetermine the time at which the overlapping area of tiles is equal to the query value, queries[i].

![img](https://s3.amazonaws.com/hr-challenge-images/5519/1422784979-db005a0a44-drawing-3.svg)

Note: Assume all distances are in meters, time in seconds and velocities in meters per second.

Function Description

Complete the *movingTiles* function in the editor below.

*movingTiles* has the following parameter(s):

- *int l:* side length for the two squares
- *int s1:* velocity of square 1
- *int s2:* velocity of square 2
- *int queries[q]:* the array of queries

Returns

- *int[n]:* an array of answers to the queries, in order. Each answer will be considered correct if it is at most 0.0001 away from the true answer.

Input Format
First line contains integers l, s1, s2.
The next line contains q, the number of queries.
Each of the next q lines consists of one integer queries[i] in one line.

Constraints
1 ≤ l, s1, s2 ≤ 10^9
1 ≤ q ≤ 10^5
1 ≤ queries[i] ≤ L^2
s1 ne s2

Sample Input

10 1 2
2
50
100

Sample Output

4.1421
0.0000

Explanation

For the first case, note that the answer is around `4.1421356237...`, so any of the following will be accepted:

4.1421356237
4.14214
4.14215000
4.1421
4.1422

Cod sursă

#include<bits/stdc++.h>
using namespace std;
int main(){
long s1,s2,L,q,Q;
double l,d;
cin>>L>>s1>>s2;
cin>>Q;
while(Q--){
    cin>>q;
    d = L * sqrt(2);
    l = sqrt(q) * sqrt(2);
    printf("%.10lf\n",(d - l)/fabs(s2-s1));
}
}
HackerRank Fundamentals – Sherlock and Moving Tiles