1. Member
Join Date
Nov 2010
Posts
2
Rep Power
0

## I need some help please !

I have a problem to solve in java. It goes like this : For a natural number n, find the maximum natural number m formed with the same digits. (example: n=3658, m=8653).

i have some part of it solved (i think) in C++ but i don't know how to change/finish it in java.

int x = 2574853;
int[] lx= new int[100];
int n = 0;
int aux = 0;
int y = 0;

while (x >0)
{
lx[n] = (int)x%10;
n++;
x = x/10;
}

for (int i= 0; i<n; i++)
for (int j= 0; j<n-1; j++)
if (lx[i] > lx[j])
{
aux = lx[i];
lx[i] = lx[j];
lx[j] = aux;
}

for (int i = 0; i < n; i++)
y = y * 10 + lx[i];

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
i have some part of it solved (i think) in C++ but i don't know how to change/finish it in java

In any case the task is less one of changing or finishing and more one of starting. If you are required to write a program in Java, writing one in C++ was a misstep.

So, figure out quite precisely what you are going to do. (based on what your C++ is doing possibly) The important thing is that you are clear, and can clearly describe, each step in the process. Then start writing some code: one step at a time compiling often to deal with any problems as they arise.

There is plenty of opportunity to get help here, but you have to say what your problem is! As in what (the precise steps) you are trying to do, whatcode (Java) code you have written and what compiler messages or runtime behaviour you can't understand.

Good luck.

3. Member
Join Date
Nov 2010
Posts
2
Rep Power
0

the problem is that i don't know how to resolve it (and i'm sure it's simple), i wrote the problems requirements at the beginning (the c++ attempt was not made by me), so i'm less than a newbie at this :(

4. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
8
Originally Posted by gavroche
I have a problem to solve in java. It goes like this : For a natural number n, find the maximum natural number m formed with the same digits.
Can you do this mathematically on paper?

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
To try and see what the steps are that you'll need to code, try answering the following two questions:

(1) What is the largest number you can make from the digits of 62208?

(2) How did you figure that out?

It's unlikely that you'll be able to answer the second in sufficient detail to begin coding, but a start is a start!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•