What is the most universal way to handle such a request and similar ones?
You are given 2 integers. Then you are given 2 queries. First query consists of 2 integers.
The most stupid way is to hard-code the processing of a request from one number first, then from two and no other way. But what if requests come in a different order? You can read each line, check its length and, depending on the length, apply stoi
to different parts of the line. And if the numbers in the request each time will be separated by a different number of spaces, how then to select one or two numbers from a string?
My attempt is a solution. Does it look like over-engineering? How to improve the program?
#include <vector> #include <iostream> #include <algorithm> #include <iterator> enum{FIRST, SECOND}; int query(std::vector<int>& v, int type) { int start, end; if(type == FIRST) { std::cin >> start; v.erase(v.begin() + start - 1); return 1; } if(type == SECOND) { std::cin >> start >> end; v.erase(v.begin() + start - 1, v.begin() + end - 1); return 1; } return -1; } void answer(std::vector<int>& v) { if(v.size() > 0) { std::cout << v.size() << std::endl; std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); } } int main() { int n, value; // number of elements std::vector<int> numbers; std::cin >> n; for(int i = 0; i < n; i++) { std::cin >> value; numbers.push_back(value); } query(numbers, FIRST); query(numbers, SECOND); answer(numbers); return 0; }