PDA

View Full Version : programming puzzle



MadCow108
May 30th, 2009, 01:08 PM
hi, a little puzzle for you, I hope not all know it already:


int i, n = 20;
for (i=0; i<n; i--)
{
cout << "x" << endl;
}


find three different solutions to get this code to print out 20 times x by only replacing a single character with another single character.
No adding of characters.

Usually people find two possibilities quite quickly but the third can be tricky :)

Victor Liu
May 31st, 2009, 11:31 AM
int i, n = 20;
for (i=0; i<n; n--)
{
cout << "x" << endl;
}



int i, n = 20;
for (i=0; i+n; i--)
{
cout << "x" << endl;
}



int i, n = 20;
for (i=0;-i<n; i--)
{
cout << "x" << endl;
}

NielsBhor
May 31st, 2009, 11:59 AM
Victor got it. Very good programmer. I didn't know about Slerp until now.