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

Thread: Fun C++ Problem

  1. #46
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

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

  2. #47
    Join Date
    Oct 2003
    Location
    Merate-North Italy
    Posts
    230

    Re: Fun C++ Problem

    Solution 4:

    Code:
    i=0;~i<n;i--
    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)

  3. #48
    Join Date
    May 2006
    Location
    Mumbai, India
    Posts
    292

    Re: Fun C++ Problem

    Same Is the caser with me .....
    Found these two first....

    i=0;i<n;n--
    i=0;-i<n;i--
    Never thought of termination condition....

    Gud one...
    Guys post more puzzles of these kinds....

  4. #49
    Join Date
    Oct 2006
    Posts
    2

    Red face 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?

  5. #50
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    164

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

  6. #51
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: Fun C++ Problem

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

  7. #52
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Fun C++ Problem

    Yes, it's a strange interview question.
    My hobby projects:
    www.rclsoftware.org.uk

  8. #53
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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.

  9. #54
    Join Date
    Mar 2005
    Location
    Canada
    Posts
    15

    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.
    Matthew Doucette / Xona Games
    ...our upcoming dual play Xbox 360 2D shooter: Duality ZF

  10. #55
    Join Date
    May 2009
    Posts
    1

    Re: Fun C++ Problem

    Three, it is very tricky question

  11. #56
    Join Date
    Aug 2010
    Posts
    3

    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 =)

  12. #57
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Fun C++ Problem

    Quote Originally Posted by pinchharmonic View Post
    i+n was definitely the hardest one! remembering that the loop condition is only false when negative helped =)
    Congratulations that you found it (which I didn't ), but the loop condition is only false when zero!

    Ah, and: Welcome to CG!

  13. #58
    Join Date
    Nov 2011
    Posts
    2

    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. : )

  14. #59
    Join Date
    Aug 2009
    Posts
    440

    Re: Fun C++ Problem

    Got all three. Surprisingly I don't think the third solution took me as long as the second solution.

  15. #60
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Fun C++ Problem

    Quote Originally Posted by whitevandal View Post
    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...

Page 4 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