Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印的格式与stdin读取的格式完全相同_Java_Arrays_String_Bigdecimal_Selection Sort - Fatal编程技术网

Java 打印的格式与stdin读取的格式完全相同

Java 打印的格式与stdin读取的格式完全相同,java,arrays,string,bigdecimal,selection-sort,Java,Arrays,String,Bigdecimal,Selection Sort,我是Java的初学者,我遇到了一个问题。问题是: 给定一个由n个实数字符串组成的数组s,按降序对它们进行排序-但是等等,还有更多!每个数字的打印格式必须与从stdin读取的格式完全相同,即.1打印为.1,0.1打印为0.1。如果两个数字表示数值相等的值(例如),则它们必须按照作为输入接收的相同顺序列出 我编写了以下代码: 'public static void main(String []args){ //Input Scanner sc= new Scanner(System.

我是Java的初学者,我遇到了一个问题。问题是:

给定一个由n个实数字符串组成的数组s,按降序对它们进行排序-但是等等,还有更多!每个数字的打印格式必须与从stdin读取的格式完全相同,即.1打印为.1,0.1打印为0.1。如果两个数字表示数值相等的值(例如),则它们必须按照作为输入接收的相同顺序列出

我编写了以下代码:

'public static void main(String []args){
    //Input
    Scanner sc= new Scanner(System.in);
    int n=sc.nextInt();
    String []s=new String[n+2];
    for(int i=0;i<n;i++){
        s[i]=sc.next();
    }
    sc.close();
    //TypeCasting
    BigDecimal[] deci = new BigDecimal[n];
    for(int i=0;i<n;i++){
        deci[i] =new BigDecimal(s[i]);
    }
    //Write your code here//SelectionSort
    for(int i =0; i<n-1; i++){
        for(int j=i; j<n; j++){
            int minIndex = i;
            if(deci[j].compareTo(deci[minIndex])>0){
                minIndex = j;
            } 
            BigDecimal temp = deci[i];
            deci[i] = deci[minIndex];
            deci[minIndex] = temp;
        }
    }
    for(int i=0;i<n;i++){
        s[i] = (deci[i]).toString();
    }
    //Output
    for(int i=0;i<n;i++)
    {
        System.out.println(s[i]);
    }
}`
'publicstaticvoidmain(字符串[]args){
//输入
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
字符串[]s=新字符串[n+2];

对于(int i=0;i您可以在
BigDecimal[]deci
的同时对
String[]s
进行排序。这将允许您从
System.in
中读取原始字符串,而不是以某种方式将
BigDecimal
对象转换回其原始字符串

您应该知道,您的代码确实实现了选择排序,但是它没有实现稳定的选择排序。也就是说,“如果两个数字表示数值相等的值(例如),那么它们必须按照作为输入接收的相同顺序列出。”
例如:

Sample Input:  6 .1 1 0.1 10 00.1 2
Expected Output: 10 2 1 .1  0.1 00.1
Your Output:     10 2 1 .1 00.1  0.1
publicstaticvoidmain(字符串[]args){
类数据{
整数阶;
双重价值;
字符串str;
}
尝试(扫描仪扫描=新扫描仪(System.in)){
scan.useLocale(Locale.ENGLISH);
比较器SortByValueDescentByOrderAsc=(一,二)->{
int res=Double.compare(两个值,一个值);
返回res==0?整数。比较(1.order,2.order):res;
};
字符串[]nums=scan.nextLine().split(\\s+);
List res=new ArrayList();
对于(int i=0;idata.str).collect(collector.joining(“”));
}
}

您为什么要编写自己的排序代码?您已经有很多排序例程可供使用。不要再发明轮子了。我对Java这类学习是新手,还没有学会。
static String[] selectionSortStable(BigDecimal[] deci, String[] s, int n) {
    //Write your code here //SelectionSort
    for(int i = 0; i < n - 1; i++) {
        for(int j = i + 1; j < n; j++) {
            int minIndex = i;
            
            if(deci[j].compareTo(deci[minIndex]) >= 0) {
                minIndex = j;
            } else {
                continue; // don't need to swap already sorted elements
            }
            
            // in order to ensure a stable sort, we need to swap unsorted
            // elements until deci[minIndex] is in deci[i]
            for(int k = minIndex; k > i; k--) {
                BigDecimal bdtmp = deci[k];
                deci[k] = deci[k - 1];
                deci[k - 1] = bdtmp;
                
                // sort the array of string representations at the same time
                String stmp = s[k];
                s[k] = s[k - 1];
                s[k - 1] = stmp;
            }
        }
        
    }
    
    return s;
}

public static void main(String[] args) {
    //Input
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String[] s = new String[n+2];
    for(int i = 0; i < n; i++) {
        s[i] = sc.next();
    }
    sc.close();
    //TypeCasting
    BigDecimal[] deci = new BigDecimal[n];
    for(int i = 0; i < n; i++) {
        deci[i] = new BigDecimal(s[i]);
    }
    
    s = selectionSortStable(deci, s, n);

    // now just output the sorted string array 's'
    for(int i = 0; i < n; i++) {
        System.out.print(s[i] + " ");
    }
    
    System.out.println();
}
public static void main(String[] args) {
    class Data {

        int order;
        double value;
        String str;
    }

    try (Scanner scan = new Scanner(System.in)) {
        scan.useLocale(Locale.ENGLISH);

        Comparator<Data> sortByValueDescThenByOrderAsc = (one, two) -> {
            int res = Double.compare(two.value, one.value);
            return res == 0 ? Integer.compare(one.order, two.order) : res;
        };

        String[] nums = scan.nextLine().split("\\s+");
        List<Data> res = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            Data data = new Data();
            data.str = nums[i];
            data.order = i;
            data.value = Double.parseDouble(data.str);
            res.add(data);
        }

        res.sort(sortByValueDescThenByOrderAsc);

        System.out.println(res.stream().map(data -> data.str).collect(Collectors.joining(" ")));
    }
}