Sam's house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam's house.
In the diagram below:
s
is the start point,t
is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.a
, and the orange tree is at point b
.d
units of distance from its tree of origin along the x
-axis. A negative value of d
means the fruit fell d
units to the tree's left, and a positive value of d
means it falls d
units to the tree's right. // Input
7 11
5 15
3 2
-2 2 1
5 -6
// Output
1
1
s
와 t
사이에 떨어진 사과와 오렌지의 개수를 구하는 문제이다.d
는 나무로부터 사과 혹은 오렌지가 떨어진 거리이며, 음수는 왼쪽 방향이다.a
와 b
각각으로부터 apples[]
, oranges[]
와의 합을 구하고, 결과 값을 s
, t
와 비교하면 될 것 같다.function countApplesAndOranges(s, t, a, b, apples, oranges) {
let appleCount = 0;
let orangeCount = 0;
for(let i = 0; i < apples.length; i++) {
const appleDropZone = a + apples[i];
if(appleDropZone >= s && appleDropZone <= t) appleCount++;
}
for(let j = 0; j < oranges.length; j++) {
const orangeDropZone = b + oranges[j];
if(orangeDropZone >= s && orangeDropZone <= t) orangeCount++;
}
console.log(appleCount);
console.log(orangeCount);
}