문제
Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where s is the start point, and t is the endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You can 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.
Apple and orange(2).png
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.
Given the value of d for m apples and n oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range [s,t])?
Function Description
Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam's house, each on a separate line.
Input Format
The first line contains two space-separated integers denoting the respective values of s and t.
The second line contains two space-separated integers denoting the respective values of a and b.
The third line contains two space-separated integers denoting the respective values of m and n.
The fourth line contains m space-separated integers denoting the respective distances that each apple falls from point a.
The fifth line contains n space-separated integers denoting the respective distances that each orange falls from point b.
Output Format
Print two integers on two different lines:
1. The first integer: the number of apples that fall on Sam's house.
2. The second integer: the number of oranges that fall on Sam's house.
CODE
void countApplesAndOranges(int s, int t, int a, int b, int apples_count, int* apples, int oranges_count, int* oranges) {
int count1=0, count2 = 0;
for (int i=0;i<apples_count;i++) // 집 안에 떨어진 사과의 개수 세기
if ((apples[i]>=(s-a))&&(apples[i]<=(t-a))) count1++; // 범위 안에 있으면 count 증가
printf("%d\n", count1); // 출력
for (int i=0;i<oranges_count;i++) // 집 안에 떨어진 오렌지의 개수 세기
if ((oranges[i]>=(s-b))&&(oranges[i]<=(t-b))) count2++; // 범위 안에 있으면 count 증가
printf("%d\n", count2); // 출력
}
감상
count 하는 변수를 하나만 쓰기 위해서 한 번 개수를 세고 for문 int i 옆에 count=0으로 초기화해서 풀려고 했다. case 0, 1, 2는 통과했지만 그 외의 case는 실패했다. 왤까?
'문제풀이 > C 문제풀이' 카테고리의 다른 글
[HackerRank]Kangaroo (0) | 2019.07.11 |
---|---|
[HackerRank]Breaking the Records (0) | 2019.07.08 |
[HackerRank]Grading Students (0) | 2019.07.01 |
[HackerRank]Time Conversion (0) | 2019.06.27 |
[HackerRank]Birthday Cake Candles (0) | 2019.06.26 |