编写一个函数以接受整数,并从输入数字升序返回由数字组成的数字

时间:2018-07-06 09:43:00

标签: java

如何编写一个函数来接受int并从输入数字中以升序返回数字组成的数字? 另外,如果输入为负,则将顺序更改为降序。

例如:

  1. 输入为48455,输出应为44558,
  2. 输入为-15243,输出应为-54321。

2 个答案:

答案 0 :(得分:1)

我尝试了代码,但未进行优化,因此我要求更好的代码。

2018-07-06 05:32:19,152 [django.request] ERROR: Internal Server Error: /cuckoo/api/status
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/cuckoo/web/utils.py", line 55, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/http.py", line 45, in inner
    return func(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/cuckoo/web/controllers/cuckoo/api.py", line 45, in status
    temp_file = Files.temp_put("")
  File "/usr/local/lib/python2.7/dist-packages/cuckoo/common/files.py", line 97, in temp_put
    prefix="upload_", dir=path or temppath()
  File "/usr/lib/python2.7/tempfile.py", line 314, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags)
  File "/usr/lib/python2.7/tempfile.py", line 244, in _mkstemp_inner
    fd = _os.open(file, flags, 0600)
OSError: [Errno 2] No such file or directory: '/tmp/cuckoo-tmp-root/upload_IUQt4r'
[06/Jul/2018 05:32:19] "POST /analysis/api/tasks/recent/ HTTP/1.1" 200 13
[06/Jul/2018 05:32:19] "GET /cuckoo/api/status HTTP/1.1" 500 12976

答案 1 :(得分:0)

在我的代码上,首先应该更改然后添加数字符号,以便通过使用Integer.parseInt()对其进行成功排序,为此,我使用了以下代码:

    if (number.contains("-")) {
        number = number.replace("-", "");
        System.out.println("-" + Reverse.sortString(initializer(number)));
    } 

方法String[] initializer(String num)用于将String文字转换为String数组。

方法String sortString(String initializer[])用于通过使用Bubble sort算法对字符串数组进行排序,根据需要,将数字从大到小排序非常重要。

方法String getSortedString(String initializer[])用于获取排序的字符串。

import java.util.Scanner;

public class Reverse {

    static String sortedString = "";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter number");
        //input
        String number = scanner.next();
        //check if input number is negative or positive
        //if negative, remove a sign and add it last.
        //else, do nothing to number
        if (number.contains("-")) {
            number = number.replace("-", "");
            System.out.println("-" + Reverse.sortString(initializer(number)));
        } else {
            System.out.println(Reverse.sortString(initializer(number)));
        }
    }

    //convert String literal to String array
    private static String[] initializer(String num) {
        String arr[] = new String[num.length()];
        for (int i = 0; i < num.length(); i++) {
            arr[i] = "" + num.charAt(i);
        }
        return arr;
    }

    //sort converted String array with using Bubble sorting
    private static String sortString(String initializer[]) {
        for (int i = 0; i < initializer.length; i++) {
            for (int j = 1; j < initializer.length; j++) {
                if (Integer.parseInt(initializer[j - 1]) < Integer.parseInt(initializer[j])) {
                    String temp = initializer[j - 1];
                    initializer[j - 1] = initializer[j];
                    initializer[j] = temp;
                }
            }
        }
        return getSortedString(initializer);
    }

    //this method is for to keep sortString on new String
    private static String getSortedString(String initializer[]) {
        for (String s : initializer) {
            sortedString += s;
        }
        return sortedString;
    }
}

更新

@Vishal Tiwari,在您添加了注释的代码上,您可以在if(a>0){...}条件内添加以下代码:

    if (a > 0) {
        StringBuffer Sorted = new StringBuffer(new String(charArray));
        return Integer.parseInt("" + Sorted.reverse());
    }

并删除这些无用的行:

    while(a!=0){
        size++;
        a /= 10;
    }