Java 如何识别代码失败的边缘情况?

Java 如何识别代码失败的边缘情况?,java,graph-theory,breadth-first-search,traveling-salesman,Java,Graph Theory,Breadth First Search,Traveling Salesman,我正在研究一个编码问题,我得到了一个数组,例如:[1,7,3,21,13,19] 我假设将数组中的项配对。然后,我想应用这个简单的规则 假设我选择一对x和y: 规则: 如果x>y:y=2*y和x=x-y 如果y>x:x=2*x且y=y-x 如果x==y:break#x和y是不会导致无限循环的一对 例如: 假设x=7,y=3 第一轮:x=7,y=3 第二轮:x=4和y=6 第三轮:x=8,y=2 第四轮:x=6,y=4 在这一点上,你知道这对将永远循环 例如,如果x=1,y=3 第一轮:x=1和y

我正在研究一个编码问题,我得到了一个数组,例如:[1,7,3,21,13,19]

我假设将数组中的项配对。然后,我想应用这个简单的规则

假设我选择一对x和y:

规则:

  • 如果x>y:y=2*y和x=x-y

  • 如果y>x:x=2*x且y=y-x

  • 如果x==y:break#x和y是不会导致无限循环的一对

  • 例如:

    假设x=7,y=3

    第一轮:x=7,y=3

    第二轮:x=4和y=6

    第三轮:x=8,y=2

    第四轮:x=6,y=4

    在这一点上,你知道这对将永远循环

    例如,如果x=1,y=3

    第一轮:x=1和y=3

    第二轮:x=2和y=2

    在这一点上,你知道这一对不会循环

    所以为了解决这个问题。我将其视为某种TSP,但不是最小化路径,而是最大化路径

    所以我做的第一步是创建一个节点图,并标记一对是否为循环

    在此数组中,生成的图形(邻接矩阵)如下所示:

    图中的索引表示数组中的索引。例如,假设i是行,j是列。如果你看i=0,j=2,它表示数组[i]=x,数组[j]=y,它是数组[0]=x=1,数组[2]=y=3。从上面的例子中我们知道这不是一个循环。因此,该指数的权重为0

    然后,我做了一个最近邻TSP算法(修改为max route),以获得最大化循环对数量的max对。此方法通过了除一个之外的编码测试用例。我无法识别一个整数数组,它会使我的代码失败。关于编码挑战的测试用例没有给我任何关于它失败的测试的信息

    这是我的密码:

    import java.util.HashMap;
    public class DistractTheGuards {
    
    private static boolean IsPairLoop(int first, int second)
    {
        long result = first + second;
        boolean success = true;
        while ((result & 1) == 0)
        {
            if (first == second)
            {
               success = false;
               break;
            }
            else if (first > second)
            {
                first = (first - second) / 2;
            }
            else
            {
                second = (second - first) / 2;
            }
            result = first + second;
        }
        return success;
    }
    
    public static void GenWeights(int[][] graph, int[] banana_list)
    {
        for (int i = 0; i < graph.length; ++i)
        {
            for (int j = 0; j < graph.length; ++j)
            {
                if (IsPairLoop(banana_list[i], banana_list[j]))
                {
                    graph[i][j] = 1;
                }
                else
                {
                    graph[i][j] = 0;
                }
            }
        }
    }
    
    private static boolean AreAllNodesVisited(boolean[] visited)
    {
        boolean all_visited = true;
        for (int i = 0; i < visited.length; ++i)
        {
            all_visited &= visited[i];
            if (!all_visited)
                break;
        }
        return all_visited;
    }
    
    private static int FindMaxTourKey(HashMap<Integer, int[]> tours)
    {
        int cur_max_r = -1;
        for (Integer rank : tours.keySet())
        {
            if (cur_max_r < rank)
                cur_max_r = rank;
        }
    
        return cur_max_r;
    }
    
    private static int GetN(int[][] graph, int[] max_tour, int n)
    {
        for (int i = 0; i < max_tour.length; i += 2)
        {
            if (i + 1 >= max_tour.length)
                break;
            if (graph[max_tour[i]][max_tour[i+1]] == 0)
            {
                n -= 2;
            }
        }
        return n;
    }
    
    public static int answer(int[] banana_list)
    {
        int n = banana_list.length;
    
        if (n < 1)
            return 0;
        if (n == 1)
            return 1;
    
        int[][] graph = new int[n][n];
        GenWeights(graph, banana_list);
        HashMap<Integer, int[]> tours = new HashMap<>();
    
        for (int i = 0; i < n; ++i)
        {
            int[] cur_tour = new int[n];
            boolean[] visited = new boolean[n];
            int start_node = i;
            int cur_tour_i = 0;
    
            while (!AreAllNodesVisited(visited))
            {
                int s_n = start_node;
                visited[start_node] = true;
                cur_tour[cur_tour_i++] = start_node;
                int cur_max = 0;
                for (int j = 0; j < n; ++j)
                {
                    if (!visited[j])
                    {
                        if (cur_max < graph[start_node][j])
                        {
                            cur_max = graph[start_node][j];
                            start_node = j;
                            break;
                        }
                    }
                }
    
                if (s_n == start_node)
                {
                    for (int x = n - 1; x >= 0; --x)
                    {
                        if (!visited[x])
                        {
                            start_node = x;
                            break;
                        }
                    }
                }
            }
    
            int cur_tour_r = 0;
            for (int x = 0; x < n; x += 2)
            {
                if (x + 1 >= n)
                    break;
                cur_tour_r += graph[cur_tour[x]][cur_tour[x+1]];
            }
    
            tours.put(cur_tour_r, cur_tour.clone());
            if (cur_tour_r == n - 1)
                break;
        }
    
        int cur_max_r = FindMaxTourKey(tours);
    
        if (tours.size() == 0)
            return 0;
    
        int[] max_tour = tours.get(cur_max_r);
    
        return GetN(graph, max_tour, n);
       }
    }
    
    import java.util.HashMap;
    公营保安{
    私有静态布尔IsPairLoop(int-first,int-second)
    {
    长结果=第一个+第二个;
    布尔成功=真;
    而((结果&1)==0)
    {
    如果(第一个==第二个)
    {
    成功=错误;
    打破
    }
    else if(第一个>第二个)
    {
    第一个=(第一秒)/2;
    }
    其他的
    {
    第二=(第二-第一)/2;
    }
    结果=第一次+第二次;
    }
    回归成功;
    }
    公共静态权重(int[][]图形,int[]香蕉列表)
    {
    对于(int i=0;i=最大行程长度)
    打破
    如果(图[max_tour[i]][max_tour[i+1]]==0)
    {
    n-=2;
    }
    }
    返回n;
    }
    公共静态整数应答(整数[]香蕉列表)
    {
    int n=香蕉列表长度;
    if(n<1)
    返回0;
    如果(n==1)
    返回1;
    int[][]图形=新的int[n][n];
    GenWeights(图表、香蕉列表);
    HashMap tours=新建HashMap();
    对于(int i=0;i=0;--x)
    {
    如果(!已访问[x])
    {
    开始_节点=x;
    打破
    }
    }
    }
    }
    int cur\u tour\r=0;
    对于(int x=0;x=n)
    打破
    cur_tour_r+=图形[cur_tour[x]][cur_tour[x+1]];
    }
    tours.put(cur_-tour,cur_-tour.clone());
    如果(cur_tour_r==n-1)
    打破
    }
    int cur_max_r=FindMaxTourKey(旅游);
    如果(tours.size()==0)
    返回0;
    int[]max\u tour=tours.get(cur\u max\u r);
    返回GetN(图,最大行程,n);
    }
    }
    
    我只需要帮助确定一个可能会使我的方法失败的边缘案例。有谁能帮助我或给我一个数组,这肯定会使我的方法失败?我可以从那里拿走。谢谢

    更新

    约束条件


  • 1您是否尝试过空数组、空数组、充满负值的数组等?确定边缘大小写是计算机科学,而不是编程。@Scottship对,是的,我忘了提到有限制。我现在已经把它们放在那里了。我假设规则是独占的(所以如果1为真,则不检查规则2)。我不及格
    import java.util.HashMap;
    public class DistractTheGuards {
    
    private static boolean IsPairLoop(int first, int second)
    {
        long result = first + second;
        boolean success = true;
        while ((result & 1) == 0)
        {
            if (first == second)
            {
               success = false;
               break;
            }
            else if (first > second)
            {
                first = (first - second) / 2;
            }
            else
            {
                second = (second - first) / 2;
            }
            result = first + second;
        }
        return success;
    }
    
    public static void GenWeights(int[][] graph, int[] banana_list)
    {
        for (int i = 0; i < graph.length; ++i)
        {
            for (int j = 0; j < graph.length; ++j)
            {
                if (IsPairLoop(banana_list[i], banana_list[j]))
                {
                    graph[i][j] = 1;
                }
                else
                {
                    graph[i][j] = 0;
                }
            }
        }
    }
    
    private static boolean AreAllNodesVisited(boolean[] visited)
    {
        boolean all_visited = true;
        for (int i = 0; i < visited.length; ++i)
        {
            all_visited &= visited[i];
            if (!all_visited)
                break;
        }
        return all_visited;
    }
    
    private static int FindMaxTourKey(HashMap<Integer, int[]> tours)
    {
        int cur_max_r = -1;
        for (Integer rank : tours.keySet())
        {
            if (cur_max_r < rank)
                cur_max_r = rank;
        }
    
        return cur_max_r;
    }
    
    private static int GetN(int[][] graph, int[] max_tour, int n)
    {
        for (int i = 0; i < max_tour.length; i += 2)
        {
            if (i + 1 >= max_tour.length)
                break;
            if (graph[max_tour[i]][max_tour[i+1]] == 0)
            {
                n -= 2;
            }
        }
        return n;
    }
    
    public static int answer(int[] banana_list)
    {
        int n = banana_list.length;
    
        if (n < 1)
            return 0;
        if (n == 1)
            return 1;
    
        int[][] graph = new int[n][n];
        GenWeights(graph, banana_list);
        HashMap<Integer, int[]> tours = new HashMap<>();
    
        for (int i = 0; i < n; ++i)
        {
            int[] cur_tour = new int[n];
            boolean[] visited = new boolean[n];
            int start_node = i;
            int cur_tour_i = 0;
    
            while (!AreAllNodesVisited(visited))
            {
                int s_n = start_node;
                visited[start_node] = true;
                cur_tour[cur_tour_i++] = start_node;
                int cur_max = 0;
                for (int j = 0; j < n; ++j)
                {
                    if (!visited[j])
                    {
                        if (cur_max < graph[start_node][j])
                        {
                            cur_max = graph[start_node][j];
                            start_node = j;
                            break;
                        }
                    }
                }
    
                if (s_n == start_node)
                {
                    for (int x = n - 1; x >= 0; --x)
                    {
                        if (!visited[x])
                        {
                            start_node = x;
                            break;
                        }
                    }
                }
            }
    
            int cur_tour_r = 0;
            for (int x = 0; x < n; x += 2)
            {
                if (x + 1 >= n)
                    break;
                cur_tour_r += graph[cur_tour[x]][cur_tour[x+1]];
            }
    
            tours.put(cur_tour_r, cur_tour.clone());
            if (cur_tour_r == n - 1)
                break;
        }
    
        int cur_max_r = FindMaxTourKey(tours);
    
        if (tours.size() == 0)
            return 0;
    
        int[] max_tour = tours.get(cur_max_r);
    
        return GetN(graph, max_tour, n);
       }
    }