-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAppleandOrange.java
More file actions
37 lines (31 loc) ยท 1.22 KB
/
AppleandOrange.java
File metadata and controls
37 lines (31 loc) ยท 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package hackerrank;
import java.util.Arrays;
public class AppleandOrange {
static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
//s์ t์ฌ์ด์ ๋จ์ด์ง ์ฌ๊ณผ์ ์ค๋ ์ง ๊ฐฏ์๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์ .
//int[] apples: ๊ฐ๊ฐ n๊ฐ์ ์ฌ๊ณผ๊ฐ ๋จ์ด์ง ์์น์ด๊ณ a์์น๊ฐ์ ๋ํด์ค๋ค
//int[] oranges: n๊ฐ์ ์ค๋์ง๊ฐ ๋จ์ด์ง ์์น์ด๊ณ b์์น๊ฐ์ ๋ํด์ค๋ค.
//์ฐ์ฐํ ๋ ๋ฐฐ์ด์ ์์๊ฐ s์ t์ฌ์ด์ ์๋ค๋ฉด ๊ฐ๊ฐ์ ๊ฐฏ์๋ฅผ ์ถ๋ ฅํ๋ค.
int numOfapples = 0;
int numOforanges = 0;
for (int i = 0; i < apples.length; i++) {
apples[i] += a;
if (apples[i] >= s && apples[i] <= t) {
numOfapples++;
}
}
for (int j = 0; j < oranges.length; j++) {
oranges[j] += b;
if (oranges[j] >= s && oranges[j] <= t) {
numOforanges++;
}
}
System.out.println(numOfapples+"\n"+numOforanges);
}
public static void main(String[] args) {
countApplesAndOranges(7, 10, 4, 12, new int[]{2, 3, -4}, new int[]{3, -2, -4});
System.out.print(", ans: 1 2\n");
countApplesAndOranges(5, 15, 3, 2, new int[]{-2, 2, 1}, new int[]{5, -6});
System.out.print(", ans: 1 1\n");
}
}