混淆整数a的每个数字,并打印出可能的最大整数。

huangapple go评论61阅读模式
英文:

Scramble each digit of the int a and print out the biggest possible integer

问题

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);
            
            }
            }
    }
英文:

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&lt;10)
             System.out.println(a);
       for(int i= 0;i&lt;aS.length();i++){
            String firstNum = aS.substring(i,i+1);
            for (int j = ah; j &gt; Integer.parseInt(firstNum); j--){
                System.out.println(ah);
            
            }
            }
    } ```

</details>


# 答案1
**得分**: 1

```java
public static int biggest(int num) {
    if (num == 0)
        return 0;

    int res = 0;

    if (num > 0) {
        for (int i = 9; i >= 0; i--)
            res = update(res, i, num);
    } else {
        for (int i = 0; i <= 9; i++)
            res = update(res, i, num);

        res *= -1;
    }

    return res;
}

private static int update(int res, int i, int n) {
    n = Math.abs(n);

    while (n > 0) {
        if (n % 10 == i)
            res = res * 10 + i;
        n /= 10;
    }

    return res;
}

Output:

System.out.println(biggest(12341234));  // 44332211
System.out.println(biggest(-12341234)); // -11223344
英文:
public static int biggest(int num) {
    if (num == 0)
        return 0;

    int res = 0;

    if (num &gt; 0) {
        for (int i = 9; i &gt;= 0; i--)
            res = update(res, i, num);
    } else {
        for (int i = 0; i &lt;= 9; i++)
            res = update(res, i, num);

        res *= -1;
    }

    return res;
}

private static int update(int res, int i, int n) {
    n = Math.abs(n);

    while (n &gt; 0) {
        if (n % 10 == i)
            res = res * 10 + i;
        n /= 10;
    }

    return res;
}

Output:

System.out.println(biggest(12341234));  // 44332211
System.out.println(biggest(-12341234)); // -11223344

答案2

得分: 0

以下是翻译好的内容:

不需要在这种情况下将其转换为字符串,您可以通过对 10 取模来获得输入数字的各个位数,然后除以 10 并重复此过程,直到数字大于 0。

每个位数应存储在一个数组或列表中。

要获取这些位数中的最大数字,您只需对它们进行排序(诸如 Arrays.sortCollections.sort 等标准工具即可),然后通过将最低位数乘以 1、10、100 等来“重新组装”最大数字,并求和。

因此,简单的实现如下所示:

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 来实现此任务,并采用相同的方法:

public static int biggestStream(int a) {

    AtomicInteger p = new AtomicInteger(1); // 累积 10 的幂次

    return IntStream.iterate(a, n -> n > 0, n -> n / 10) // 在数字大于 0 时将输入数字除以 10
                    .map(i -> (i % 10)) // 获取位数
                    .sorted() // 排序(从低位数开始)
                    .map(i -> p.getAndUpdate((x) -> x * 10) * i) // 与上述 p * digit 相同
                    .sum(); // 获取结果数字
}

更新
从 '9' 到 '0' 迭代位数,并检查它们是否在输入数字的字符串表示中可用。

基于 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));
}
英文:

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.

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&lt;Integer&gt; digits = new ArrayList&lt;&gt;();
    while (a &gt; 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;
}

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 -&gt; n &gt; 0, n -&gt; n / 10) // divide input number by 10 while it &gt; 0
                    .map(i -&gt; (i % 10)) // get the digit
                    .sorted() // sort (the lower digits first)
                    .map(i -&gt; p.getAndUpdate((x) -&gt; x * 10) * i) // same as p * digit above
                    .sum(); // get the result number
}

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

String-based solution:

public static void biggest(int a) {

    String aS = String.valueOf(a);
    if (a &lt; 10) {
        System.out.println(a);
    }
    String num = &quot;&quot;;
    int count = 0;
    out: for (char i = &#39;9&#39;; i &gt;= &#39;0&#39;; i--) {
        for (int j = 0; j &lt; aS.length(); j++) {
            char digit = aS.charAt(j);
            if (digit == i) {
                num += digit;
                if (++count == aS.length()) {
                    break out;
                }
            }
        }
    }
    System.out.println(num + &quot; / &quot; + Integer.parseInt(num));
}

答案3

得分: 0

另一个选项是计算有多少个0、1、2、...、9的值,然后将它们按照降序(9、8、7、...、0)重新组合成一个数字。实现这个的简单方法是使用数组。因为这是一个作业任务,按照你在评论中添加的要求,困难的方法(不使用数组)是为每个数字使用一个变量计数器。

public class so64125767 {
    public static int biggestBuckets(int a) {
        int[] buckets = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        while (a > 0) {
            buckets[a % 10]++;
            a /= 10;
        }

        int num = 0;
        for (int i = 9; i >= 0; i--) {
            for (int j = 0; j < buckets[i]; j++) {
                num *= 10;
                num += i;
            }
        }

        return num;
    }

    public static int biggestBucketsVar(int a) {
        int zero = 0;
        int one = 0;
        int two = 0;
        int three = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int seven = 0;
        int eight = 0;
        int nine = 0;

        while (a > 0) {
            switch (a % 10) {
                case 0:
                    zero++;
                    break;
                case 1:
                    one++;
                    break;
                case 2:
                    two++;
                    break;
                case 3:
                    three++;
                    break;
                case 4:
                    four++;
                    break;
                case 5:
                    five++;
                    break;
                case 6:
                    six++;
                    break;
                case 7:
                    seven++;
                    break;
                case 8:
                    eight++;
                    break;
                case 9:
                    nine++;
                    break;
            }
            a /= 10;
        }

        int num = 0;

        for (int j = 0; j < nine; j++) {
            num *= 10;
            num += 9;
        }

        // 以下省略其他数字的处理

        return num;
    }

    public static void main(String[] args) {
        System.out.println(biggestBuckets(237428379));
        System.out.println(biggestBucketsVar(237428379));
        -- 987743322
    }
}

我敢打赌,如果你对这些结果以及其他建议(使用String或Collections)进行基准测试,你会发现这种方法的性能表现最佳(想象一下如果你接受超出int大小范围的数字)。

英文:

Another option would be to count how many 0, 1, 2, ..., 9 values you have and then assemble them back together into a number knowing the digits will always be in descending order (9, 8, 7, ..., 0). The easy way to do this is with an array. Since this is a homework assignment the hard way (without using an array as per the requirement you added in a comment) is to use a variable counter per digit.

public class so64125767 {
public static int biggestBuckets(int a) {
int[] buckets = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
while (a &gt; 0) {
buckets[a % 10]++;
a /= 10;
}
int num = 0;
for (int i = 9; i &gt;= 0; i--) {
for (int j = 0; j &lt; buckets[i]; j++) {
num *= 10;
num += i;
}
}
return num;
}
public static int biggestBucketsVar(int a) {
int zero = 0;
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
while (a &gt; 0) {
switch (a % 10) {
case 0:
zero++;
break;
case 1:
one++;
break;
case 2:
two++;
break;
case 3:
three++;
break;
case 4:
four++;
break;
case 5:
five++;
break;
case 6:
six++;
break;
case 7:
seven++;
break;
case 8:
eight++;
break;
case 9:
nine++;
break;
}
a /= 10;
}
int num = 0;
for (int j = 0; j &lt; nine; j++) {
num *= 10;
num += 9;
}
for (int j = 0; j &lt; eight; j++) {
num *= 10;
num += 8;
}
for (int j = 0; j &lt; seven; j++) {
num *= 10;
num += 7;
}
for (int j = 0; j &lt; six; j++) {
num *= 10;
num += 6;
}
for (int j = 0; j &lt; five; j++) {
num *= 10;
num += 5;
}
for (int j = 0; j &lt; four; j++) {
num *= 10;
num += 4;
}
for (int j = 0; j &lt; three; j++) {
num *= 10;
num += 3;
}
for (int j = 0; j &lt; two; j++) {
num *= 10;
num += 2;
}
for (int j = 0; j &lt; one; j++) {
num *= 10;
num += 1;
}
for (int j = 0; j &lt; zero; j++) {
num *= 10;
// num += 0;
}
return num;
}
public static void main(String[] args) {
System.out.println(biggestBuckets(237428379));
System.out.println(biggestBucketsVar(237428379));
-- 987743322
}
}

I'm also going to bet if you benchmark these results along with the other suggestions (using String or Collections) you'll find this method scales the best (imagine if you accepted numbers beyond the size of an int).

答案4

得分: -1

    String useMe = Integer.toString(argumentOne);
int rMe = argumentOne;
int x = 0;
while (x != 1000) {
int i = 0;
String returnMe = "";
String inUse = useMe;
while (i != useMe.length()) {
Random random = new Random();
int index = random.nextInt(inUse.length());
returnMe = returnMe + inUse.charAt(index);
inUse = inUse.substring(0, index) + inUse.substring(index + 1);
i++;
}
if (Integer.parseInt(returnMe) > rMe) {
rMe = Integer.parseInt(returnMe);
}
x++;
}
System.out.print(rMe);
}
英文:
    String useMe = Integer.toString(argumentOne);
int rMe = argumentOne;
int x = 0;
while (x != 1000) {
int i = 0;
String returnMe = &quot;&quot;;
String inUse = useMe;
while (i != useMe.length()) {
Random random = new Random();
int index = random.nextInt(inUse.length());
returnMe = returnMe + inUse.charAt(index);
inUse = inUse.substring(0, index) + inUse.substring(index + 1);
i++;
}
if (Integer.parseInt(returnMe) &gt; rMe) {
rMe = Integer.parseInt(returnMe);
}
x++;
}
System.out.print( rMe );
}
</details>

huangapple
  • 本文由 发表于 2020年9月30日 02:45:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64125767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定