해커랭크_easy 10p_Apple and Orange_구현

RostoryT·2023년 1월 14일
0

String and Implementation

목록 보기
18/18





파라미터 설명

  • Complete the 'countApplesAndOranges' function below.
  • The function accepts following parameters:
    1. INTEGER s : starting point of Sam's house location.
    1. INTEGER t : ending location of Sam's house location.
    1. INTEGER a : location of the Apple tree.
    1. INTEGER b : location of the Orange tree.
    1. INTEGER_ARRAY apples : distances at which each apple falls from the tree.
    1. INTEGER_ARRAY oranges : distances at which each orange falls from the tree.

솔루션 - 내가 푼

  • What should I do for this
  • A. Simple solution (by tae)
    1. Calculate the distance d_i for each apples from a and If d_i inside of range (between s and t), then APPLE++
    1. Calculate the distance d_j for each oranges from b and If d_j inside of range (between s and t), then ORANGE++
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
 * Complete the 'countApplesAndOranges' function below.
 *
 * The function accepts following parameters:
 *  1. INTEGER s                : starting point of Sam's house location.
 *  2. INTEGER t                : ending location of Sam's house location.
 *  3. INTEGER a                : location of the Apple tree.
 *  4. INTEGER b                : location of the Orange tree.
 *  5. INTEGER_ARRAY apples     : distances at which each apple falls from the tree.
 *  6. INTEGER_ARRAY oranges    : distances at which each orange falls from the tree.
 */

 /*
  *   What should I do for this
  *   A. Simple solution (by tae)
  *   1. Calculate the distance d_i for each apples from a and If d_i inside of range (between s and t), then APPLE++
  *   2. Calculate the distance d_j for each oranges from b and If d_j inside of range (between s and t), then ORANGE++
  */
void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
	int APPLE = 0, ORANGE = 0;
	// 1.
	for (int app : apples) {
		if (s <= app + a && app + a <= t) APPLE++;
	}

	// 2.
	for (int org : oranges) {
		if (s <= org + b && org + b <= t) ORANGE++;
	}

	cout << APPLE << "\n" << ORANGE;
}

int main()
{
	string first_multiple_input_temp;
	getline(cin, first_multiple_input_temp);

	vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));

	int s = stoi(first_multiple_input[0]);

	int t = stoi(first_multiple_input[1]);

	string second_multiple_input_temp;
	getline(cin, second_multiple_input_temp);

	vector<string> second_multiple_input = split(rtrim(second_multiple_input_temp));

	int a = stoi(second_multiple_input[0]);

	int b = stoi(second_multiple_input[1]);

	string third_multiple_input_temp;
	getline(cin, third_multiple_input_temp);

	vector<string> third_multiple_input = split(rtrim(third_multiple_input_temp));

	int m = stoi(third_multiple_input[0]);

	int n = stoi(third_multiple_input[1]);

	string apples_temp_temp;
	getline(cin, apples_temp_temp);

	vector<string> apples_temp = split(rtrim(apples_temp_temp));

	vector<int> apples(m);

	for (int i = 0; i < m; i++) {
		int apples_item = stoi(apples_temp[i]);

		apples[i] = apples_item;
	}

	string oranges_temp_temp;
	getline(cin, oranges_temp_temp);

	vector<string> oranges_temp = split(rtrim(oranges_temp_temp));

	vector<int> oranges(n);

	for (int i = 0; i < n; i++) {
		int oranges_item = stoi(oranges_temp[i]);

		oranges[i] = oranges_item;
	}

	countApplesAndOranges(s, t, a, b, apples, oranges);

	return 0;
}

string ltrim(const string &str) {
	string s(str);

	s.erase(
		s.begin(),
		find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
	);

	return s;
}

string rtrim(const string &str) {
	string s(str);

	s.erase(
		find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
		s.end()
	);

	return s;
}

vector<string> split(const string &str) {
	vector<string> tokens;

	string::size_type start = 0;
	string::size_type end = 0;

	while ((end = str.find(" ", start)) != string::npos) {
		tokens.push_back(str.substr(start, end - start));

		start = end + 1;
	}

	tokens.push_back(str.substr(start));

	return tokens;
}

profile
Do My Best

0개의 댓글