(I posted this on CodeReview as well, I hope it's ok. I would love to get different perspectives & suggestion across different platforms).
I am following a question on a programming challenge website
(here's the original reference). What I have is a
house (start & endpoint) and some types of
trees at their respective location points. Each of these trees produce some
fruits that would fall at different
distances. My job is to find if the fruits have fallen on the house area (inside of start & endpoint). Illustration is
here.
Instead of `cin` everything into a single var (according to the illustration), I want to make my code more dynamic, therefore I decided to use vector structure following
a suggestion by @CPallini on my prev question.
house = location point of house (start & endpoint)
trees = location point of each tree
fruits = number of fruits fall
fruit = distance of each fallen fruit
counting = number of fruits fall inside the house area
Constraints :
1 <= house, trees, fruits, distance <= 10^5
-10^5 <= distance <= 10^5
a < s < t < b (following the picture)
Input Format :
Sample Input : (sample test case)
7 11 // house location
5 15 // tree location
3 2 // how many fruits fall from each tree
-2 2 1 // distance of 3 fruits fell from tree A
5 -6 // distance of 2 fruits fell from tree A
Sample Output :
1 // tree loc = 5 , fruits loc : -2 2 1. So, (5-2=3) (5+2=7 - inside house!) (5+1 =6)
1 // tree loc = 15, fruits loc : 5 -6. So, (15+5=20) (15-6=9 - inside house!)
What I have tried:
My code is a working code, it works on a several test cases (as in sample input), but failed some cases (sample inputs) as well, including
this and
this. Any improvement I can do with my code or advice on better approach ? Here's my code :
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using std::cout;
using std::cin;
using std::getline;
using std::cerr;
using std::exit;
using std::vector;
using std::string;
using std::stringstream;
using std::istringstream;
vector<int> house; vector<int> trees; vector<int> fruits; vector<vector <int>> fruit_loc; vector<int> fruit_onsite;
istringstream iss;
const double max_value = 100000;
const double min_value = -100000;
void reset(){
string line;
int data = 0;
iss.clear();
iss.seekg(0);
line.clear();
}
void input(vector<int>& input){ string line;
int data = 0;
getline(cin, line);
iss.str(line);
while (iss >> data){
if ((data >= 1) && (data <= max_value)){
input.push_back(data);
} else {
cerr << "input is less than 1 or bigger than 10^5";
exit(1); }
}
}
void input_tree(vector<int>& input,vector<int>& house){
string line;
int data = 0;
getline(cin, line);
iss.str(line);
while (iss >> data){
if (((data < house[0]) || (data > house[1])) && ((data >= 1) && (data <= max_value))){
input.push_back(data);
} else {
cerr << "Tree can't be inside the house area !";
exit(1); }
}
}
void print(vector<int>& input){
for (size_t i =0; i < input.size(); i++){
cout << "Input " << input[i] << " ";
}
cout << "\n";
}
int main(){
int count = 0;
string line;
int data = 0;
input(house);
reset();
input_tree(trees, house);
reset();
input(fruits);
for (size_t i = 0; i < fruits.size(); i++){
reset();
fruit_loc.push_back(vector<int>());
getline(cin, line);
iss.str(line);
for (int j = 0; j < fruits[i]; j++){
while (iss >> data){
if ((data >= min_value) && (data <= max_value)){
fruit_loc[i].push_back(data);
if ((trees[0] + data >= house[i]) && (trees[i] + data <= house[1])) count++;
} else {
cerr << "input is less than -10^5 or bigger than 10^5";
exit(1); }
}
}
fruit_onsite.push_back(count);
}
for (auto value: fruit_onsite){
cout << value << "\n";
}
return 0;
}