|
-
September 29th, 2009, 08:12 PM
#1
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
-
September 29th, 2009, 08:18 PM
#2
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.
-
September 29th, 2009, 08:33 PM
#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++?
-
September 29th, 2009, 08:38 PM
#4
Re: HELP me OUT please..
 Originally Posted by vastolorde88
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.
-
September 29th, 2009, 08:49 PM
#5
Re: HELP me OUT please..
 Originally Posted by vastolorde88
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.
-
September 29th, 2009, 09:13 PM
#6
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?
-
September 30th, 2009, 08:37 AM
#7
-
September 30th, 2009, 08:41 AM
#8
-
September 30th, 2009, 10:41 AM
#9
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
-
September 30th, 2009, 11:32 AM
#10
Re: HELP me OUT please..
 Originally Posted by vastolorde88
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".
}
}
}
}
-
September 30th, 2009, 01:37 PM
#11
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!
-
September 30th, 2009, 04:39 PM
#12
Re: HELP me OUT please..
 Originally Posted by Ajay Vijay
Now you are ready! 
With what?
There one solution and it's 6174 = 7641 - 1467.
-
September 30th, 2009, 04:41 PM
#13
Re: HELP me OUT please..
Oh wow, that's going to be such a learning experience then.
-
September 30th, 2009, 06:20 PM
#14
Re: HELP me OUT please..
 Originally Posted by Lindley
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.
-
September 30th, 2009, 07:22 PM
#15
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?
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
|