[cpp]
do {
if( j % 2 > 0 ) {
j= ( 3 * j ) + 1;
x++;
} else {
j = j / 2;
x++;
} // if
} while( j > 1 );
[/cpp]
So when j = 1, the flow of the program enters the loop and j goes from 1 -> 4 -> 2 -> 1 and x = 4 when it exits the do-while loop.
In the while loop version, the condition ( j > 1 ) is false, and the program flow doesn't enter the loop, leaving x with a value of 1. (Which is the correct answer).
You probably should consult a C++ reference about namespaces. In short, they are used to control the scope of what is declared in the headers files, i.e. where and how you can use the functions, classes, objects, etc. that are declared in the headers. This is useful if you have a large amount of code, where two methods could end up with the same name (which would normally cause compile errors). For example, we can have two variables called number if we put them in different namespaces:
[cpp]
#include <iostream>
// without this we would need to be explicit and use "std::cout" instead of "cout"
using namespace std;
namespace Alpha {
int number = 2;
}; // namespace Alpha
namespace Beta {
int number = 4;
}; // namespace Beta
int
main( void )
{
cout << "Alpha: " << Alpha::number << endl;
cout << "Beta: " << Beta::number << endl;
return 0;
}
[/cpp]
And this will print out:
Namespaces are kind of new, so your code will compile and work without the line [cpp]using namespace std;[/cpp] (At least with the version of the g++ compiler currently used by the judge).Alpha: 2
Beta: 4