CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29
  1. #16
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: HELP me OUT please..

    Quote Originally Posted by nuzzle View Post
    With what?

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

    My post was relevant to the experience level of OP. He/She should know how to play with numbers, how to get the indivisual digits, and process them. Of course, the tasks I gave was not relevant to the original question in hand, the OP mentioned himself to be "noob in C++"
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  2. #17
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: HELP me OUT please..

    Quote Originally Posted by dc_2000 View Post
    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?
    Well if "we" want to escape this, probably everyone have to ignore such topics as
    * i am noob
    * please help, urgent
    and so on, and so on. All these questions are not informative, and probably its homework.
    Share and always try to give back more.

  3. #18
    Join Date
    Sep 2002
    Location
    Czech Republic
    Posts
    47

    Re: HELP me OUT please..

    This is the code for the ''brute force" solution. I'm not including the implementation of the MaxSorted() and MinSorted() functions because giving the whole thing out to you without any effort from your side would be quite uneducational.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int MaxSorted( int );
    int MinSorted( int );
    void Swap( int&, int& );
    
    int main(int argc, char *argv[])
    {
       int i;
    
       for( i=1000; i < 10000; i++ )
       {
            if( i == MaxSorted( i ) - MinSorted( i ) )
            {
               printf( "%d\n", i);
            }
       }
            
        getch();
        return 0;    
    }

    To give you a hint, in these functions individual digits are extracted from the number and put into an array which is then sorted and then the return value is created using this array.


    This is how you extract the individual numbers ( integer division and modulo operators are used ):

    Code:
        Arr[0] =   Num % 10;
        Arr[1] = ( Num % 100 ) / 10;
        Arr[2] = ( Num % 1000 ) / 100;
        Arr[3] =   Num / 1000;

    Next you must sort the array. I wrote bubble sort algorithm for this. But you can use anything you like. You may even find a sorting function in a library.


    After sorting the array you create the MaxSorted numer:

    Code:
        return 1000*Arr[3] + 100*Arr[2] + 10*Arr[1] + Arr[0];

    The MinSorted number is created by a minor change of the MaxSorted version. Just figure it out.

    ... and the result is really 6174. Programming is fun, just give it a try and do some research if you're not sure about something. I think I've given you enough hints,

    regards

    Koxin

  4. #19
    Join Date
    May 2009
    Posts
    2,413

    Re: HELP me OUT please..

    Quote Originally Posted by Koxin View Post
    This is the code for the ''brute force" solution.
    It's hardly THE code for the brute force solution. It's one approach among several. I've suggested another one in #10 which avoids extracting the individual digits from the candidate numbers.

    Besides your code has a logical bug. You need to test that all digits of a candidate number are unique. This follows from the a<b<c<d restriction.

    Also there's no need to sort twice. One sort in either ascending or descending order is enougth. The other order is just the array read in reverse. A full general sort algoritm isn't necessary. 6 conditional swaps sorts a 4 member array (in the case the OP isn't allowed to use standard sort).

  5. #20
    Join Date
    Sep 2002
    Location
    Czech Republic
    Posts
    47

    Re: HELP me OUT please..

    The code uses the "brute force" approach. It can be optimized and shortened. But who needs that? It yields the correct result anyway.

  6. #21
    Join Date
    May 2009
    Posts
    2,413

    Re: HELP me OUT please..

    Quote Originally Posted by Koxin View Post
    The code uses the "brute force" approach. It can be optimized and shortened. But who needs that? It yields the correct result anyway.
    The way you expressed yourself suggests there's only one brute force solution namely the one supplied by you. This is not true, there are several.

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

    Re: HELP me OUT please..

    And you claim I state the obvious just to have something to say?

  8. #23
    Join Date
    Sep 2009
    Posts
    88

    Re: #10 Solving analytically

    I'd really like to understand the thought process for obtaining those 21 numbers.

  9. #24
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: HELP me OUT please..

    CBV. It is very unlikely that nuzzle figured anything out on his own. This problem is related to what is called the Kaprekar Routine if you are curious.
    Last edited by souldog; October 2nd, 2009 at 02:15 AM.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

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

    Re: #10 Solving analytically

    Quote Originally Posted by cbv View Post
    I'd really like to understand the thought process for obtaining those 21 numbers.
    Well I tried to find a solution with pen and paper but didn't get that far really. Anyway you have this relation,

    a < b < c < d

    Where a, b, c and d are digits in any order in a number N. Now the problem is to find an N which equals this number M,

    M = (d*1000 + c*100 + b*10 + a) - (a*1000 + b*100 + c*10 + d)

    If you manipulate M algebraically you can get this,

    M = (d-a)*999 + (c-b)*90

    Now (d-a) and (c-b) are always positive because of a < b < c < d. That relation also says that b and c lies between a and d.

    So (d-a) can vary between 8 (9-1) and 3 (because b and c lies between a and d). And with similar reasoning one can convince oneself that (c-b) can vary between 1 and (d-a)-2. In code that would be,

    Code:
    for (int i=3; i<=8; ++i) { // variation of (d-a)
       for (int j=1; j<=i-2; ++j) { // variation of (c-b)
          int m = i*999 + j*90;
          std::cout << m << "\n";
       }
    }
    Last edited by nuzzle; October 2nd, 2009 at 05:10 AM.

  11. #26
    Join Date
    May 2009
    Posts
    2,413

    Re: HELP me OUT please..

    Quote Originally Posted by souldog View Post
    CBV. It is very unlikely that nuzzle figured anything out on his own. This problem is related to what is called the Kaprekar Routine if you are curious.
    I had no idea that this was a known problem with a name. Then at least it's probably hard and I don't have to feel that bad I was unable to come up with a mathematical solution.

    Anyways I came as far as to this formulation,

    Find a solution to

    (d-a)*999 + (c-b)*90 = x*1000 + y*100 + z*10 + w

    where a, b, c, d are digits with a<b<c<d,

    and where x,y,z,w is a permutation of a,b,c,d.
    Last edited by nuzzle; October 2nd, 2009 at 02:58 AM.

  12. #27
    Join Date
    Sep 2009
    Posts
    88

    Re: HELP me OUT please..

    Can you explain this part more: "(c-d) can vary between 1 and (d-a)-2"? Wouldn't c-d be negative?

  13. #28
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: HELP me OUT please..

    You can also take the problem the other way round, and iterate over all a,b,c,d with a<b<c<d, and then verify then their diference is actually writable in an abcd form:

    PHP Code:
    int main()
    {
        for(
    unsigned int a 1a<=6; ++a)
        {
            for(
    unsigned int b a+1b<=7; ++b)
            {
                for(
    unsigned int c b+1c<=8; ++c)
                {
                    for(
    unsigned int d c+1d<=9; ++d)
                    {
                        
    int aBig = ((d*10+c)*10+b)*10+a;
                        
    int aSmall = ((a*10+b)*10+c)*10+d;
                        
    int aDiff aBig aSmall;
                        if (
    isMadeOf(aDiffa,b,c,d))
                        {
                            
    std::cout << aDiff << " == " << aBig << " - " << aSmall << std::endl;
                        }
                    }
                }
            }
        }
        return 
    0;

    basically, you just need to check that aDiff is made of a,b,c and d.
    One of the advantages here is that there are only 121 abcd numbers where a<b<c<d.
    The other is that it requires no sort or nothing complicated.

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

    Re: HELP me OUT please..

    Quote Originally Posted by cbv View Post
    Can you explain this part more: "(c-d) can vary between 1 and (d-a)-2"? Wouldn't c-d be negative?
    Sorry it's a typo. I changed it my previous post.

    It should be "(c-b) can vary between 1 and (d-a)-2".

Page 2 of 2 FirstFirst 12

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