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

给你一囗甜甜゛ 提交于 2021-02-05 12:34:30

问题


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);
            
            }
            }
    } ```

回答1:


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<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;
}

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
}

Update
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 < 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));
}



回答2:


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



回答3:


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 > 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;
        }
        
        for (int j = 0; j < eight; j++) {
            num *= 10;
            num += 8;
        }

        for (int j = 0; j < seven; j++) {
            num *= 10;
            num += 7;
        }

        for (int j = 0; j < six; j++) {
            num *= 10;
            num += 6;
        }

        for (int j = 0; j < five; j++) {
            num *= 10;
            num += 5;
        }

        for (int j = 0; j < four; j++) {
            num *= 10;
            num += 4;
        }

        for (int j = 0; j < three; j++) {
            num *= 10;
            num += 3;
        }

        for (int j = 0; j < two; j++) {
            num *= 10;
            num += 2;
        }

        for (int j = 0; j < one; j++) {
            num *= 10;
            num += 1;
        }

        for (int j = 0; j < 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:


    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 );
 
  }


来源:https://stackoverflow.com/questions/64125767/scramble-each-digit-of-the-int-a-and-print-out-the-biggest-possible-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!