m개, n개의 과일들이 떨어진 위치들 중 나무의 위치를 포함했을 때 s와 t사이에 떨어진 각각의 과일들의 수를 구하는 countApplesAndOranges 함수를 완성 시켜라.
해결방법.
*Array.FindAll 사용.
apple의 떨어진 위치 중 나무의 위치 a를 더했을 떄 s와 t사이에 포함되는 수를 찾는다.
오렌지도 마찬가지.
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution {
static int[] appleAndOrange(int s, int t, int a, int b, int[] apple, int[] orange) { // Complete this function int[] count = new int[2]; count[0] = Array.FindAll<int>(apple, obj => obj + a >= s && obj + a <= t).Length; count[1] = Array.FindAll<int>(orange, obj => obj + b >= s && obj + b <= t).Length; return count; }
static void Main(String[] args) { string[] tokens_s = Console.ReadLine().Split(' '); int s = Convert.ToInt32(tokens_s[0]); int t = Convert.ToInt32(tokens_s[1]); string[] tokens_a = Console.ReadLine().Split(' '); int a = Convert.ToInt32(tokens_a[0]); int b = Convert.ToInt32(tokens_a[1]); string[] tokens_m = Console.ReadLine().Split(' '); int m = Convert.ToInt32(tokens_m[0]); int n = Convert.ToInt32(tokens_m[1]); string[] apple_temp = Console.ReadLine().Split(' '); int[] apple = Array.ConvertAll(apple_temp,Int32.Parse); string[] orange_temp = Console.ReadLine().Split(' '); int[] orange = Array.ConvertAll(orange_temp,Int32.Parse); int[] result = appleAndOrange(s, t, a, b, apple, orange); Console.WriteLine(String.Join("\n", result)); } }