HR - Apple & Orange


質問する


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:
  • The red region denotes the house, where s is the start point,
    and t is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.
  • Assume the trees are located on a single point, where the apple tree is at point a , and the orange tree is at point b .
  • When a fruit falls from its tree, it lands 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

    に答える

  • Samのスペースstの間に落ちたリンゴとオレンジの個数.
  • d木からリンゴかオレンジの距離しかなく、負の値は左側です.
  • リンゴ樹とオレンジ樹の位置abはそれぞれapples[]oranges[]を求め、結果値をstと比較した.
  • コード#コード#

    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);
    
    }