Solving algorithm problems in C++