CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Join Date
    Sep 2009
    Posts
    3

    HELP me OUT please..

    im a noob in c++ and i have a project to pass this weekend.. and i badly need help..

    help me out please..

    i need to:

    find a 4-digit number that the difference between their digits arranged from greatest to smallest and smallest to greatest is the original number. (a<b<c<d) - (d<c<b<a) = original number.

    please hep me out..

    T____T

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: HELP me OUT please..

    Fortunately, there are only 9000 4-digit numbers. That means you can easily solve this problem via brute force----simply search until you find a value which fits the criteria.

    So all you need to do is:
    1) Write a function to split out an integer into an array of its individual digits. This is pretty easy, there are hundreds of examples around the web.
    2) Be able to sort the digits each way. This is again pretty easy if you leverage std::sort().
    3) Write a function to combine the digits in the array into a single value again.
    4) Subtract the two values so obtained and compare the difference to the original.

  3. #3
    Join Date
    Sep 2009
    Posts
    3

    Re: HELP me OUT please..

    omg.. nosebleed terms..

    im really a noob at using c++.. can you at least give me some codes or program lines in c++?

  4. #4
    Join Date
    Sep 2009
    Posts
    19

    Re: HELP me OUT please..

    Quote Originally Posted by vastolorde88 View Post
    omg.. nosebleed terms..

    im really a noob at using c++.. can you at least give me some codes or program lines in c++?
    My father can do this better, as he claims he is an expert of C then C++. I will introcude this to him for a within-family run show case

    I advise a book by Barry Moose (?) Accelerated C++. a Cool informative book with various basic applications included
    The color blending is an indication of the underlying prostitution system.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: HELP me OUT please..

    Quote Originally Posted by vastolorde88 View Post
    omg.. nosebleed terms..

    im really a noob at using c++.. can you at least give me some codes or program lines in c++?
    It's only Tuesday. You have plenty of time.

    This board isn't here to do your homework for you.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: HELP me OUT please..

    The key is to tackle one small problem at a time, rather than trying to solve the whole thing at once. What's the very first thing you need to do which you don't understand?

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: HELP me OUT please..

    Look up the "for" loop

  8. #8
    Join Date
    Sep 2009
    Posts
    3

    Re: HELP me OUT please..

    "for" loop?!

  9. #9
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: HELP me OUT please..

    Brute force means trying all possible combinations without a smart algorithm. So you try all numbers from 1000 to 9999 and check if any of these meets the required condition.
    So you have to loop over all numbers from 1000 to 9999, that´s what you need the for loop for. That´s C++ fundamental.
    - Guido

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: HELP me OUT please..

    Quote Originally Posted by vastolorde88 View Post
    omg.. nosebleed terms..

    im really a noob at using c++.. can you at least give me some codes or program lines in c++?
    Note that a 4-digit number xyzw can be constructed from the individual digits x, y, z and w with this formula: x*1000 + y*100 + z*10 + w. To utilize this to generate all numbers between 1000 and 9999 you use 4 nested loops, like
    Code:
    for (int x=1; x<10; ++x) {
       for (int y=0; y<10; ++y) {
          for (int z=0; z<10; ++z) {
             for (int w=0; w<10; ++w) {
                int n = x*1000 + y*100 + z*10 + w; 
                // n will take all numbers between 1000 and 9999
                // and x, y, z, w are available to construct 
                // the other numbers required to determine
                // whether n is a "hit".
             }
          }
       }
    }

  11. #11
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: HELP me OUT please..

    First do this:
    1. Take number from user, and display how many digits are in number. 715 has 3 digits.
    2. Display all numbers separated by , (comma) - 7, 1, 5
    3. Display the sum of all numbers - 13
    4. Reverse the number, and display the difference (715-517) = 198

    Now you are ready!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  12. #12
    Join Date
    May 2009
    Posts
    2,413

    Re: HELP me OUT please..

    Quote Originally Posted by Ajay Vijay View Post
    Now you are ready!
    With what?

    There one solution and it's 6174 = 7641 - 1467.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: HELP me OUT please..

    Oh wow, that's going to be such a learning experience then.

  14. #14
    Join Date
    May 2009
    Posts
    2,413

    Re: HELP me OUT please..

    Quote Originally Posted by Lindley View Post
    Oh wow, that's going to be such a learning experience then.
    Well, everybody should know that if they post homework here it may get stolen and made right in front of their eyes and there's nothing they can do about it!

    I tried to solve it analytically on paper but only came as far as that the correct number(s) must appear in this list,
    Code:
    for (int i=3; i<=8; ++i) {
       for (int j=1; j<=i-2; ++j) {
          int m = i*999 + j*90;
          std::cout << m << "\n";
       }
    }
    There were 21 numbers on the list so I checked them (skipping numbers with double digits because they're invalid). And there it was 6174.
    Last edited by nuzzle; September 30th, 2009 at 06:31 PM.

  15. #15
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: HELP me OUT please..

    I've looked through this forum and it has become a "help-me-with-my-homework" life line these days. How did you find it, vastolorde88?

Page 1 of 2 12 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