C++ solution for problem 20