[HackerRank]Apple and Orange
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 is the start point, and 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 , and the orange tree is at point .
When a fruit falls from its tree, it lands units of distance from its tree of origin along the -axis. A negative value of means the fruit fell units to the tree's left, and a positive value of means it falls units to the tree's right.
Complete the function countApplesAndOranges
,
where,
Starting point of Sam's house location.
Ending location of Sam's house location.
Location of the Apple tree.
Location of the Orange tree.
Number of apples that fell from the tree.
Distance at which each apple falls from the tree.
Number of oranges that fell from the tree.
Distance at which each orange falls from the tree.
Given the value of for apples and oranges, can you determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )? Print the number of apples that fall on Sam's house as your first line of output, then print the number of oranges that fall on Sam's house as your second line of output.
Input Format
The first line contains two space-separated integers denoting the respective values of and .
The second line contains two space-separated integers denoting the respective values of and .
The third line contains two space-separated integers denoting the respective values of and .
The fourth line contains space-separated integers denoting the respective distances that each apple falls from point .
The fifth line contains space-separated integers denoting the respective distances that each orange falls from point .
Constraints
Output Format
Print two lines of output:
- On the first line, print the number of apples that fall on Sam's house.
- On the second line, print the number of oranges that fall on Sam's house.
Sample Input 0
7 11
5 15
3 2
-2 2 1
5 -6
Sample Output 0
1
1
Explanation 0
The first apple falls at position .
The second apple falls at position .
The third apple falls at position .
The first orange falls at position .
The second orange falls at position .
Only one fruit (the second apple) falls within the region between and , so we print as our first line of output.
Only the second orange falls within the region between and , so we print as our second line of output.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <bits/stdc++.h> using namespace std; vector <int> appleAndOrange(int s, int t, int a, int b, vector <int> apple, vector <int> orange) { int cnt_apple = 0; int cnt_orange = 0; for(int i=0; i<apple.size(); i++){ if(a+apple[i] >= s && a+apple[i]<=t) cnt_apple++; } for(int i=0; i<orange.size(); i++){ if(b+orange[i] >= s && b+orange[i]<=t) cnt_orange++; } vector<int> result; result.push_back(cnt_apple); result.push_back(cnt_orange); return result; } int main() { int s; int t; cin >> s >> t; int a; int b; cin >> a >> b; int m; int n; cin >> m >> n; vector<int> apple(m); for(int apple_i = 0; apple_i < m; apple_i++){ cin >> apple[apple_i]; } vector<int> orange(n); for(int orange_i = 0; orange_i < n; orange_i++){ cin >> orange[orange_i]; } vector <int> result = appleAndOrange(s, t, a, b, apple, orange); for (ssize_t i = 0; i < result.size(); i++) { cout << result[i] << (i != result.size() - 1 ? "\n" : ""); } cout << endl; return 0; } | cs |
Comment
입력인자가 많긴 하지만.. 어쨌든 모두 필요한 인자들.
집에 떨어지는 과일의 갯수를 세는건데.. 결국 떨어지는 모든 과일마다 집의 범위 안에 떨어지는지 체크해서 카운트해준다.