-
October 17th, 2006, 11:50 PM
#1
Fun C++ Problem
Hey guys!
This is not a homework assignment or anything.
A buddy of mine who works for a software company in Michigan sent me this problem.
He said it was on their yearly technical tests that they give programmers.
I only could figure out two ways to do this... and that took me a LONG time.
As far as the third solution goes, I have no idea, and neither does my friend. I can give you the first two though later on.
take the following code:
int i, n = 20;
for (i=0; i<n; i--)
{
cout << "x" << endl;
}
by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:
find 3 ways to make the above code print x 20 times (by changing only one character).
And, according to my friend, he said his boss has been giving this problem yearly since about 1999, and no one has figured out all three ways to do it.
Good luck, this drove me nuts for a while!
Last edited by dcjr84; October 18th, 2006 at 12:18 AM.
Please rate my post if you felt it was helpful
-
October 18th, 2006, 12:10 AM
#2
Re: Fun C++ Problem
I can get two also but a 3rd is hard! I'd be fairly certain that we are probably stumped on the same thing now.
Windows XP, Visual Studio 2008, SVN
-
October 18th, 2006, 12:16 AM
#3
Re: Fun C++ Problem
Don't post any solutions yet if you have them.
I want to see how many people can actually figure out all three ways , but not until everyone has had a chance to read the problem.
Plus, it's fun to watch people "sweat" a little over a fun problem like this
Please rate my post if you felt it was helpful
-
October 18th, 2006, 01:53 AM
#4
Re: Fun C++ Problem
I have three!
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:58 AM
#5
Re: Fun C++ Problem
Can you insert a character? or is it only replace.
-
October 18th, 2006, 02:01 AM
#6
Re: Fun C++ Problem
Can you insert a character? or is it only replace.
You can only change (replace ) existing characters in the code.
Nothing can be added.
Please rate my post if you felt it was helpful
-
October 18th, 2006, 02:30 AM
#7
Re: Fun C++ Problem
This is one of the trick questions a friend of mine was asked in the interview for his job at our current companies. I remember that I needed 2 minutes to find the first two, and more than 10 minutes to find the 3rd. It's a good one!
-
October 18th, 2006, 02:34 AM
#8
Re: Fun C++ Problem
I remember that I needed 2 minutes to find the first two, and more than 10 minutes to find the 3rd. It's a good one!
Lol, I am not that quick
Took me about 1 minute to find the first one,
about 30 minutes to find the second one because it requires an odd way of thinking imo
and I don't even see how a third solution is possible, although I am sure there is one ( and possibly more ).
Please rate my post if you felt it was helpful
-
October 18th, 2006, 02:59 AM
#9
Re: Fun C++ Problem
Originally Posted by *io*
Can you insert a character? or is it only replace.
Well, I guess replacing a whitespace is inserting and since you want to insert - insert where there is a whitespace, it is replacing not inserting
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, 03:07 AM
#10
Re: Fun C++ Problem
Originally Posted by exterminator
Well, I guess replacing a whitespace is inserting and since you want to insert - insert where there is a whitespace, it is replacing not inserting
Ouch, my head hurts.
Reminds me of "how much wood could a woodchuck chuck if a woodchuck could chuck wood?"
Please rate my post if you felt it was helpful
-
October 18th, 2006, 03:11 AM
#11
Re: Fun C++ Problem
So you are allowed to "replace" a whitespace with a new character?
-
October 18th, 2006, 04:14 AM
#12
Re: Fun C++ Problem
Originally Posted by laitinen
So you are allowed to "replace" a whitespace with a new character?
Yes, otherwise there cannot be 3 solutions.
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, 04:20 AM
#13
Re: Fun C++ Problem
Originally Posted by exterminator
Yes, otherwise there cannot be 3 solutions.
Just what I thought! The first two were pretty easy, but still struggling with the last one!!
-
October 18th, 2006, 04:26 AM
#14
Re: Fun C++ Problem
Okay, I will give a hint - all three changes are at nearly the same place (same expression?)
//dcjr84 - hope this doesn't spoil your fun.. if it does .. the please don't hit me
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, 07:06 AM
#15
Re: Fun C++ Problem
Three!
Took me almost half an hour.
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
|