-
October 18th, 2006, 12:58 PM
#31
Re: Fun C++ Problem
-
October 18th, 2006, 01:26 PM
#32
Re: Fun C++ Problem
Originally Posted by RoboTact
I think I got all 3 under 10 minutes (didn't really time it).
Have you ran your program? How fast does it find the solution?
And is there a fourth one???
P.S. Sorry I couldn't rate your beautiful post. Apparently, I have to "spread" stuff...
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...
-
October 18th, 2006, 01:38 PM
#33
Re: Fun C++ Problem
It's SLOW. It only parsed 30 symbols by now. Multithreading may help here.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
October 18th, 2006, 01:52 PM
#34
Re: Fun C++ Problem
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 18th, 2006, 01:53 PM
#35
Re: Fun C++ Problem
Got all 3 It took some time for me as simultaneously solved some production issue for one of our client. Though solving this was fun than production problem
Vinod
-
October 18th, 2006, 02:13 PM
#36
Re: Fun C++ Problem
I think I found the third solution first. But overall it took me a few minutes for finding all of them, it's not that easy.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 18th, 2006, 03:33 PM
#37
Re: Fun C++ Problem
Just what I thought! The first two were pretty easy, but still struggling with the last one!!
The first two I found did not involve a whitespace. But perhaps this is a hint to the third one.....hmmm
ok people, you can go ahead and post solutions now if you have them. I think enough people have suffered
Here are the first two I found, with the character changed highlighted in red...
Took me about 30 minutes to find these two. Hopefully this is never asked in an interview because I would probably not get the job lol.
1)
Code:
int i, n = 20;
for (i=0; i<n; n--)
{
cout << "x" << endl;
}
2)
Code:
int i, n = 20;
for (i=0; i+n; i--)
{
cout << "x" << endl;
}
But for the love of all that is holy what is the third solution!?!?!?!?
Last edited by dcjr84; October 18th, 2006 at 03:37 PM.
Please rate my post if you felt it was helpful
-
October 18th, 2006, 03:41 PM
#38
Re: Fun C++ Problem
Negating a negative gives you a positive.
Code:
int i, n = 20;
for (i=0;-i<n; i--)
{
cout << "x" << endl;
}
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 18th, 2006 at 03:44 PM.
-
October 18th, 2006, 03:43 PM
#39
Re: Fun C++ Problem
I got nr 1 and 3, but nr 2?? Still cant see that!! Have to look closer at it tomorrow! When is that actually teminating?
-
October 18th, 2006, 03:44 PM
#40
Re: Fun C++ Problem
OMG! Noooooooooooooooo.....
I was totally thinking that for the third solution and was going to check it, but then my hot pockets were ready from the microwave, and when I came back I lost my train of thought.
Nice job everyone, this was fun!
Gonna ask my buddy to send me more of those whenever he gets them
Last edited by dcjr84; October 18th, 2006 at 03:46 PM.
Please rate my post if you felt it was helpful
-
October 18th, 2006, 03:46 PM
#41
Re: Fun C++ Problem
Originally Posted by laitinen
I got nr 1 and 3, but nr 2?? Still cant see that!! Have to look closer at it tomorrow! When is that actually teminating?
-20 + 20 = 0.
Take a look at the for loop, and the terminating condition. Since on the 20th iteration:
i + n =
-20 + 20 =
0 =
false
The loop terminates.
Regards,
Paul McKenzie
-
October 18th, 2006, 04:27 PM
#42
Re: Fun C++ Problem
Actually i got 3rd as 2nd & 2nd as 3rd
But i guess we are going 1st & 2nd based on what dcjr84 knew
Vinod
-
October 18th, 2006, 04:28 PM
#43
Re: Fun C++ Problem
I got the same three for-loop internals, in this order:
i=0;i+n;i--
i=0;-i<n;i--
i=0;i<n;n--
Interesting, it seems we were struggling to find different 3rd solutions.
Last edited by Zaccheus; October 18th, 2006 at 04:30 PM.
-
October 18th, 2006, 04:33 PM
#44
Re: Fun C++ Problem
Originally Posted by Zaccheus
I got the same three for-loop internals, in this order:
i=0;i+n;i--
i=0;-i<n;i--
i=0;i<n;n--
Interesting, it seems we were struggling to find different 3rd solutions.
You must be one smart cookie if you got the i+n first
My buddy says everyone gets these two within a reasonable amount of time
Code:
i=0;i<n;n--
i=0;-i<n;i--
But this one he said most people never figure out, and if they do it takes them a long time
But, I guess everyone thinks differently because I got the i+n second, but couldn't figure out the -i<n
Please rate my post if you felt it was helpful
-
October 18th, 2006, 04:47 PM
#45
Re: Fun C++ Problem
It took me a while to stop looking at the termination condition and consider decrementing n.
Also I was initially convinced that something could be done with the n = 20 statement, that cost me a lot of time.
Last edited by Zaccheus; October 18th, 2006 at 04:50 PM.
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
|