Java 如何识别代码失败的边缘情况?
我正在研究一个编码问题,我得到了一个数组,例如:[1,7,3,21,13,19] 我假设将数组中的项配对。然后,我想应用这个简单的规则 假设我选择一对x和y: 规则: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
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);
}
}
我只需要帮助确定一个可能会使我的方法失败的边缘案例。有谁能帮助我或给我一个数组,这肯定会使我的方法失败?我可以从那里拿走。谢谢
更新
约束条件
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);
}
}