对int a的每个数字进行加扰并打印出最大可能的整数 [英] Scramble each digit of the int a and print out the biggest possible integer

查看:41
本文介绍了对int a的每个数字进行加扰并打印出最大可能的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在这里.我只是继续制作新的字符串并将其转换为int还是我们有更快更好的方法?

I’m stuck here. Do I just keep making new strings and turn them to int or us there a faster better way?

public void biggest(int a){
       int random;
       String aS = String.valueOf(a);
       int ah=9;
       if (a<10)
             System.out.println(a);
       for(int i= 0;i<aS.length();i++){
            String firstNum = aS.substring(i,i+1);
            for (int j = ah; j > Integer.parseInt(firstNum); j--){
                System.out.println(ah);
            
            }
            }
    } ```

推荐答案

在这种情况下,无需使用转换为String的方法,您可以通过对输入的余数进行模10取余,然后将输入除以来从输入数字中获取数字.数字乘以10并在数字>0.

There's no need to use conversion to String in this case, you can get the digits from the input number by getting a remainder by modulo 10, then dividing the input number by 10 and repeat it while the number > 0.

每个数字应存储在数组或列表中.

Each digit should be stored in an array or list.

要获取最多的这些数字,您应该对它们进行排序(例如 Arrays.sort Collections.sort 这样的标准工具就可以了),然后单击重新组装"将最低位数乘以1、10、100等,然后将其相加即可得出最大的数字.

To get the biggest number of these digits you should just sort them (standard facilities such as Arrays.sort or Collections.sort will do fine) and then "re-assemble" the biggest number from the lowest digit by multiplying it by 1, 10, 100, etc. and summing up.

因此,简单的实现可能如下:

So, plain implementation could be as follows:

public static int biggestPlain(int a) {
    List<Integer> digits = new ArrayList<>();
    while (a > 0) {
        digits.add(a % 10);
        a /= 10;
    }
    Collections.sort(digits);
    int p = 1;
    int num = 0;
    for (int digit : digits) {
        num += p * digit;
        p *= 10;
    }
    return num;
}

此外,可以使用Stream API和lambda并应用相同的方法来实现此任务:

Also, this task can be implemented using Stream API and lambda and applying the same approach:

public static int biggestStream(int a) {

    AtomicInteger p = new AtomicInteger(1); // accumulate powers of 10

    return IntStream.iterate(a, n -> n > 0, n -> n / 10) // divide input number by 10 while it > 0
                    .map(i -> (i % 10)) // get the digit
                    .sorted() // sort (the lower digits first)
                    .map(i -> p.getAndUpdate((x) -> x * 10) * i) // same as p * digit above
                    .sum(); // get the result number
}

更新
在从"9"到"0"的数字之间进行迭代,并检查输入数字的字符串表示中是否可用这些数字.

Update
Iterate over digits from '9' till '0' and check if they are available in the string presentation of the input number.

String 的解决方案:

public static void biggest(int a) {

    String aS = String.valueOf(a);
    if (a < 10) {
        System.out.println(a);
    }
    String num = "";
    int count = 0;
    out: for (char i = '9'; i >= '0'; i--) {
        for (int j = 0; j < aS.length(); j++) {
            char digit = aS.charAt(j);
            if (digit == i) {
                num += digit;
                if (++count == aS.length()) {
                    break out;
                }
            }
        }
    }
    System.out.println(num + " / " + Integer.parseInt(num));
}

这篇关于对int a的每个数字进行加扰并打印出最大可能的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆