Problem #1 C++ solution