|
-
September 30th, 2009, 10:41 PM
#16
Re: HELP me OUT please..
 Originally Posted by nuzzle
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++"
-
October 1st, 2009, 07:21 AM
#17
Re: HELP me OUT please..
 Originally Posted by dc_2000
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.
-
October 1st, 2009, 10:57 AM
#18
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
-
October 1st, 2009, 11:30 AM
#19
Re: HELP me OUT please..
 Originally Posted by Koxin
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).
-
October 1st, 2009, 03:44 PM
#20
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.
-
October 1st, 2009, 04:43 PM
#21
Re: HELP me OUT please..
 Originally Posted by Koxin
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.
-
October 1st, 2009, 04:50 PM
#22
Re: HELP me OUT please..
And you claim I state the obvious just to have something to say?
-
October 2nd, 2009, 12:51 AM
#23
Re: #10 Solving analytically
I'd really like to understand the thought process for obtaining those 21 numbers.
-
October 2nd, 2009, 01:14 AM
#24
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."
-
October 2nd, 2009, 02:18 AM
#25
Re: #10 Solving analytically
 Originally Posted by cbv
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.
-
October 2nd, 2009, 02:48 AM
#26
Re: HELP me OUT please..
 Originally Posted by souldog
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.
-
October 2nd, 2009, 04:05 AM
#27
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?
-
October 2nd, 2009, 04:22 AM
#28
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 = 1; a<=6; ++a) { for(unsigned int b = a+1; b<=7; ++b) { for(unsigned int c = b+1; c<=8; ++c) { for(unsigned int d = c+1; d<=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(aDiff, a,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.
-
October 2nd, 2009, 05:12 AM
#29
Re: HELP me OUT please..
 Originally Posted by cbv
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".
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
|