import java.util.Arrays; import java.util.Scanner; public class MaxNumber { public static long print(long number) { char[] digits = Long.toString(number).toCharArray(); Arrays.sort(digits); return Long.parseLong(new StringBuilder(new String(digits)).reverse().toString()); } }
- import java.util.Arrays;
- import java.util.Scanner;
- public class MaxNumber {
- public static long print(long number) {
String numeroString = Long.toString(number);char[] digitos = numeroString.toCharArray();Arrays.sort(digitos);StringBuilder numeroOrdenadoStr = new StringBuilder(new String(digitos));numeroOrdenadoStr.reverse();return Long.parseLong(numeroOrdenadoStr.toString());- char[] digits = Long.toString(number).toCharArray();
- Arrays.sort(digits);
- return Long.parseLong(new StringBuilder(new String(digits)).reverse().toString());
- }
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("Ingrese un numero: ");long number = scanner.nextLong();long maxNumber = print(number);System.out.println("El número más alto que se puede formar con estos dígitos es: " + maxNumber);}- }
import java.math.BigInteger; import java.util.stream.Collectors; public class MaxNumber { public static BigInteger print(long number) { return new BigInteger(Long.toString(number) .chars() .mapToObj(c -> String.valueOf((char) c)) .sorted((a, b) -> b.compareTo(a)) .collect(Collectors.joining())); } }
import java.util.Arrays;import java.util.Comparator;import java.util.stream.Collectors;- import java.math.BigInteger;
- import java.util.stream.Collectors;
- public class MaxNumber {
- public static BigInteger print(long number) {
String numeroMaximo = Long.toString(number).chars().mapToObj(Character::toString).sorted((a, b) -> b.compareTo(a)).collect(Collectors.joining());return new BigInteger(numeroMaximo);- return new BigInteger(Long.toString(number)
- .chars()
- .mapToObj(c -> String.valueOf((char) c))
- .sorted((a, b) -> b.compareTo(a))
- .collect(Collectors.joining()));
- }