-
October 18th, 2006, 06:50 PM
#46
Re: Fun C++ Problem
VC++6 compiler was a lot faster (to fail on wrong code) - but still took 30 minutes on 1GHz machine. Result is that there are indeed only 3 solutions.
Code:
- at 24:
int i, n = 20;
for (i=0;-i<n; i--)
{
cout << "x" << endl;
}
+ at 26:
int i, n = 20;
for (i=0; i+n; i--)
{
cout << "x" << endl;
}
n at 30:
int i, n = 20;
for (i=0; i<n; n--)
{
cout << "x" << endl;
}
Last edited by RoboTact; October 18th, 2006 at 06:53 PM.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
October 19th, 2006, 03:19 AM
#47
Re: Fun C++ Problem
Solution 4:
this doesn't work, in fact prints x 21 times due to differences between 1 and 2 's complement.
++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
God does not play dice with the universe.(A.Einstein)
-
October 19th, 2006, 04:18 AM
#48
Re: Fun C++ Problem
Same Is the caser with me .....
Found these two first....
Never thought of termination condition....
Gud one...
Guys post more puzzles of these kinds....
-
October 19th, 2006, 05:12 AM
#49
Re: Fun C++ Problem
Hi,
A nice one, it took me about 3 minutes to get first solution, then it was easy, I could get all the three in 5 mintes.
How is it going for others?
-
October 19th, 2006, 03:54 PM
#50
Re: Fun C++ Problem
I got all three of them in a few minutes, because I thought about the loop terminating with an answer that is not Boolean.
I really need to point out that such a puzzle is a bad interview question. Such puzzles are fun, but they are not a good indication of programming ability. More importantly, as Joel Spolsky says it, it doesn't show if the candidate is smart and gets things done. I highly recommend his article:
The Guerrilla Guide to Interviewing
Finally, avoid brain teaser questions like the one where you have to arrange 6 equal length matches to make exactly 4 identical perfect triangles. If it's an "aha!" question, you don't get any information about "smart/get things done" by figuring out if they happen to make the mental leap or not.
A great read. The puzzles are fun, no doubt. I love them. Just don't judge people based on how quickly they can solve it...
-
October 19th, 2006, 05:36 PM
#51
Re: Fun C++ Problem
Originally Posted by JasonD
I really need to point out that such a puzzle is a bad interview question. Such puzzles are fun, but they are not a good indication of programming ability. More importantly, as Joel Spolsky says it, it doesn't show if the candidate is smart and gets things done.
The puzzles are fun, no doubt. I love them. Just don't judge people based on how quickly they can solve it...
Well put. I couldn't agree with you more.
-
October 20th, 2006, 02:48 AM
#52
Re: Fun C++ Problem
Yes, it's a strange interview question.
-
October 20th, 2006, 07:54 AM
#53
Re: Fun C++ Problem
I found the first solution in the first minute, then I thought 'what a pointless exercise' and went off and did something more interesting instead.
-
October 20th, 2006, 07:47 PM
#54
Re: Fun C++ Problem
I finally got all three! I'll admit that it helped when the details of the problem were explained... as it hinted at where I should look for the third solution (which was -i<n for me).
Last edited by Matthew Doucette; October 20th, 2006 at 07:54 PM.
-
May 25th, 2009, 02:58 AM
#55
Re: Fun C++ Problem
Three, it is very tricky question
-
August 24th, 2010, 04:44 PM
#56
Re: Fun C++ Problem
new here, found this fun problem through google.
i+n was definitely the hardest one! remembering that the loop condition is only false when negative helped =)
-
August 24th, 2010, 07:16 PM
#57
-
November 17th, 2011, 07:25 PM
#58
Re: Fun C++ Problem
What about
int i, n =-20;
?
Took about 5 minutes. I never would have thought of n+1. More than 3 solutions, I guess. : )
-
November 17th, 2011, 09:11 PM
#59
Re: Fun C++ Problem
Got all three. Surprisingly I don't think the third solution took me as long as the second solution.
-
November 18th, 2011, 03:35 PM
#60
Re: Fun C++ Problem
Originally Posted by whitevandal
What about
int i, n =-20;
?
Took about 5 minutes. I never would have thought of n+1. More than 3 solutions, I guess. : )
If n is -20, i=0 will never be less than n.
Interestingly, since I didn’t want to read past the first few posts, I did not realize that I played this game five years ago, and accidentally found (again) all three solutions. Again without timing; about 10 minutes, I guess.
Do I get a double bonus?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|