尽管在我生成的每个测试用例上都运行,但Open.Kattis.com上的SortofSorting在第二个测试用例上失败了。

时间:2018-04-05 19:54:55

标签: java stable-sort

我是Java编程的新手,虽然我已经用其他语言编程了几年了。我最近开始在Kattis上学习不同的课程,以便分配给我的学生。我想在分配问题之前开发解决方案手册。 (我确实认识到他们可能会发现这篇文章。请在没有提供解决方案的情况下指导我找到答案!)。问题的链接如下:

https://open.kattis.com/problems/sortofsorting

我编写了稳定的排序以满足问题的标准。我想在不使用内置函数的情况下为学生提供尽可能简单的解决方案。我与同事讨论了解决方案,但没有人能够发现阻止代码生成正确输出的极端情况/问题。

import java.util.Scanner;
import java.util.ArrayList;

public class Sorted {
    public static void main(String[] args) 
    {
        Scanner user_input = new Scanner(System.in); //User_input takes in input
        int names = user_input.nextInt(); //names is the number of names that are coming
        ArrayList<String> list = new ArrayList<String>(); //list is the list of names
        String name; //name is the current name
        int minimumIndex; //minimum index is the index of the current lowest character count
        int ascii1; //ascii value of the minimum index
        int ascii2; //ascii value of the current index
        String temp; //used to swap names in the list
        boolean first = true; //used to only print newline after each case (was used as an attempt to fix Kattis solution but not needed)
        while (names!=0) //while lists exist
        {
            if (first == false)
            {
                System.out.println(); //print blank line after each case
            }
            for(int counter = 0;counter<names;counter++)
            {
                //take in each name - done
                name = user_input.next();
                list.add(name);
            }

            //sort the names using selection sort
            for(int j = 0; j<names-1;j++)
            {
                minimumIndex = j;
                for(int i=j+1;i<names;i++)
                {
                    ascii1 = (int) list.get(minimumIndex).charAt(0);
                    ascii2 = (int) list.get(i).charAt(0);

                    if(ascii2 < ascii1) //sorting by first character
                    {
                        minimumIndex = i;
                    }
                    if(ascii1 == ascii2) //sorting by second character
                    {
                        ascii1 = (int) list.get(minimumIndex).charAt(1);
                        ascii2 = (int) list.get(i).charAt(1);
                        if(ascii2 < ascii1)
                        {
                            minimumIndex = i;
                        }
                    }
                }

                if (minimumIndex != j) //if not already smallest element, swap elements
                {
                    //perform a swap here
                    temp = list.get(j);
                    list.set(j,list.get(minimumIndex));
                    list.set(minimumIndex, temp);
                }
            }

            //output the names
            for(int counter = 0;counter<names;counter++)
            {
                System.out.println(list.get(counter));
            }
            //clear the list of elements        
            for(int counter=names-1;counter>=0;counter--)
            {
                list.remove(counter);
            }
            //get next case         
            names = user_input.nextInt();
            if (first == true)
            {
                first = false; //again, used for spacing for Kattis.
            }
        }
        //System.out.println("complete");
        //System.out.println();
        //user_input.close();
    }
}

我尝试过不同的排序,认为我对问题的解释是不正确的,包括按字母顺序排序,然后按ascii顺序排序,但这也产生了错误的答案。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你的排序不稳定。它将因此输入而失败,例如:

3
px1
px2
pa
0

最简单的解决方法是将元素移至minimumIndex,而不是使用j处的元素进行交换。

而不是:

if (minimumIndex != j) //if not already smallest element, swap elements
{
    //perform a swap here
    temp = list.get(j);
    list.set(j,list.get(minimumIndex));
    list.set(minimumIndex, temp);
}

尝试:

if (minimumIndex != j)
{
    list.add(j, list.remove(minimumIndex));
}