CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 5 FirstFirst 12345 LastLast
Results 31 to 45 of 69

Thread: Fun C++ Problem

  1. #31
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Talking Re: Fun C++ Problem

    Quote Originally Posted by exterminator
    OMG!!!

    RoboTact, for how long did you not have a good sleep? It was just a small puzzle..
    Worst of all: Didn't use std::string everywhere !!
    My hobby projects:
    www.rclsoftware.org.uk

  2. #32
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Fun C++ Problem

    Quote Originally Posted by RoboTact
    Code:
    ...
    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...

  3. #33
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    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."

  4. #34
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Fun C++ Problem

    Quote Originally Posted by RoboTact
    It's SLOW. It only parsed 30 symbols by now. Multithreading may help here.
    It is still running on my machine since I posted my last post in this thread and that is about 1 hour 10 minutes back. It hasn't found any until yet. I am going to sleep now... so will have to terminate it... sorry RoboTact for stopping your program... I should clarify that I don't mean any disrespect to it... Will try again in leisure...

    And yes, use std::string and std::vector

    //Go have some fresh air... smoke should help

  5. #35
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208

    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

  6. #36
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    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.

  7. #37
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    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

  8. #38
    Join Date
    Apr 1999
    Posts
    27,449

    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.

  9. #39
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    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?

  10. #40
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    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

  11. #41
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Fun C++ Problem

    Quote 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

  12. #42
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208

    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

  13. #43
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Talking 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.
    My hobby projects:
    www.rclsoftware.org.uk

  14. #44
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Fun C++ Problem

    Quote 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
    Code:
    i=0;i+n;i--
    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

  15. #45
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    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.
    My hobby projects:
    www.rclsoftware.org.uk

Page 3 of 5 FirstFirst 12345 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured