Skip to content

Commit 1465830

Browse files
committed
issue #24 AppleandOrange
1 parent dbe0573 commit 1465830

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package hackerrank;
2+
3+
import java.util.Arrays;
4+
5+
public class AppleandOrange {
6+
7+
static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
8+
//s์™€ t์‚ฌ์ด์— ๋–จ์–ด์ง„ ์‚ฌ๊ณผ์™€ ์˜ค๋ Œ์ง€ ๊ฐฏ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ.
9+
//int[] apples: ๊ฐ๊ฐ n๊ฐœ์˜ ์‚ฌ๊ณผ๊ฐ€ ๋–จ์–ด์ง„ ์œ„์น˜์ด๊ณ  a์œ„์น˜๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค
10+
//int[] oranges: n๊ฐœ์˜ ์˜ค๋žœ์ง€๊ฐ€ ๋–จ์–ด์ง„ ์œ„์น˜์ด๊ณ  b์œ„์น˜๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค.
11+
//์—ฐ์‚ฐํ•œ ๋‘ ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ s์™€ t์‚ฌ์ด์— ์žˆ๋‹ค๋ฉด ๊ฐ๊ฐ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
12+
int numOfapples = 0;
13+
int numOforanges = 0;
14+
for (int i = 0; i < apples.length; i++) {
15+
apples[i] += a;
16+
if (apples[i] >= s && apples[i] <= t) {
17+
numOfapples++;
18+
}
19+
}
20+
21+
for (int j = 0; j < oranges.length; j++) {
22+
oranges[j] += b;
23+
if (oranges[j] >= s && oranges[j] <= t) {
24+
numOforanges++;
25+
}
26+
}
27+
System.out.println(numOfapples+"\n"+numOforanges);
28+
}
29+
30+
public static void main(String[] args) {
31+
countApplesAndOranges(7, 10, 4, 12, new int[]{2, 3, -4}, new int[]{3, -2, -4});
32+
System.out.print(", ans: 1 2\n");
33+
34+
countApplesAndOranges(5, 15, 3, 2, new int[]{-2, 2, 1}, new int[]{5, -6});
35+
System.out.print(", ans: 1 1\n");
36+
}
37+
}

0 commit comments

Comments
ย (0)