使用数组数组(int [] []),创建一个方法来查找给定排列中的所有循环[关闭](Using an array of arrays (int[][]), create a method to fin

编程入门 行业动态 更新时间:2024-10-28 16:30:35
使用数组数组(int [] []),创建一个方法来查找给定排列中的所有循环[关闭](Using an array of arrays (int[][]), create a method to find all cycles within a given permutation [closed]) java

所以我可以为此使用标准数组,没有别的。 我必须找到一种方法来创建一个方法来查找给定排列中的所有循环,并将它们作为数组的数组对象返回。 然后我必须将每个数组的最低位置作为数组的第一个条目。 然后按最低排序。

我不能使用arraylists或集合或任何东西。

编辑:循环我的意思是取初始对象的整数值,并找到它对应的索引值。 在该索引处获取该整数值并执行相同操作。 继续这样做,直到它指向已经被引用的对象。

一个例子: [0, 4, 2, 8, 7, 9, 1, 6, 5, 3]

将是这些周期: [0] [4, 8, 5, 7, 6, 9, 3, 2] [1]

并返回: [0], [1], [2, 4, 8, 5, 7, 6, 9, 3]

要么

这个数组: [2, 4, 8, 1, 5, 3, 9, 0, 7, 6]

将是这些周期: [2, 8, 7, 0] [4, 5, 3, 1] [9, 6]

并返回: [0, 2, 8, 7], [1, 4, 5, 3], [6, 9]

我迷路了,任何帮助都会很精彩。 提前谢谢!

So I can just use standard arrays for this, nothing else. I have to find a way to make a method that finds all the cycles in the given permutation and returns them as an array object of arrays. Then I have to place the lowest of each array as the first entry of the array. Then sort them by lowest.

I can't use arraylists or sets or anything.

EDIT: By cycles I mean take the integer value of the initial object and locate the index value that it corresponds to. Take that integer value at that index and do the same. Continue doing this until it points back to an object that's already been referenced.

An example: [0, 4, 2, 8, 7, 9, 1, 6, 5, 3]

would be these cycles : [0] [4, 8, 5, 7, 6, 9, 3, 2] [1]

and return this: [0], [1], [2, 4, 8, 5, 7, 6, 9, 3]

or

This array: [2, 4, 8, 1, 5, 3, 9, 0, 7, 6]

would be these cycles : [2, 8, 7, 0] [4, 5, 3, 1] [9, 6]

and return this : [0, 2, 8, 7], [1, 4, 5, 3], [6, 9]

I am so lost, any help would be wonderful. thanks ahead of time!

最满意答案

不要问为什么我花时间这样做。

编辑:现在完全工作

public class Main { public Main() { } public static void main(String[] args) { int array[] = {0, 4, 2, 8, 7, 9, 1, 6, 5, 3}; Main m = new Main(); int[][] cycles = m.getCycles(array); for (int i = 0; i < cycles.length; i++) { System.out.print("["); for (int j = 0; j < cycles[i].length; j++) { System.out.print(cycles[i][j]); if (j < cycles[i].length - 1) System.out.print(", "); } System.out.println("]"); } System.out.println("end debug"); } public int[][] getCycles(int[] array) { int[][] cycles = new int[array.length][array.length]; // initialize the cycles to all -1s, cuz they'll never be in the array for (int i = 0; i < cycles.length; i++) { for (int j = 0; j < cycles[i].length; j++) { cycles[i][j] = -1; } } int i = 0; do { int nextElement = array[i]; int j = 0; do { cycles[i][j] = nextElement; nextElement = array[nextElement]; j++; } while (!elementInArray(cycles[i], nextElement) && j < array.length); i++; } while (!arrayHasCycled(array, cycles) && i < array.length); cycles = removeNegativeOnes(cycles, i); for (i = 0; i < cycles.length; i++) { pushForward(cycles[i]); } return cycles; } public boolean elementInArray(int[] array, int element) { for (int i = 0; i < array.length; i++) { if( array[i] == element) return true; } return false; } public int[][] removeNegativeOnes(int[][] cycles, int numCycles) { int [][] newCycles = new int[numCycles][]; for (int i = 0; i < numCycles; i++) { int realLenOfCycle = indexOf(-1, cycles[i]); newCycles[i] = new int[realLenOfCycle]; for (int j = 0; j < newCycles[i].length; j++) { newCycles[i][j] = cycles[i][j]; } } return newCycles; } public int indexOf(int element, int[] array) { int index = -1; for (int i = 0; i < array.length; i++) { if (array[i] == element) return i; } return index; } public boolean arrayHasCycled(int[] array, int[][] cycles) { for (int i = 0; i < array.length; i++) { boolean cycleHasValue = false; for (int j = 0; j < cycles.length; j++) { for (int k = 0; k < cycles[j].length; k++) { if (cycles[j][k] == array[i]) cycleHasValue = true; } } if (!cycleHasValue) return false; } return true; } public void pushForward(int [] array) { int lastElement = array[array.length - 1]; for (int i = array.length - 1; i > 0; i--) { array[i] = array[i - 1]; } array[0] = lastElement; } }

输出:

[0] [1, 4, 7, 6] [2] [3, 8, 5, 9]

据我所知,您被要求我们创建一个执行以下算法的代码:

创建一个整数的array 对于该数组中的每个元素, nextElement执行以下操作: 创建一个新的一维数组, currCycle将被添加到一个二维数组, cycles 。 将该数组的第一个元素设置为nextElement 。 nextElement然后变成array[nextElement] 如果nextElement已经在currCycle ,则继续执行array的下一个元素 检查array所有元素是否都在cycles ,如果是,则停止执行此算法。 最后,将循环作为二维数组返回,使用正在使用的索引而不是该索引处的元素,这是当前数组所包含的。 为了实现这一点,只需循环(在正常意义上)将数组的每个元素向前推送一个索引。

这并不完全符合您的示例,但我认为您的示例可能不正确,例如:

一个例子:[0,4,2,8,7,9,1,6,5,3]

将是这些周期:[0] [4,8,5,7,5,9,3,2] [1]

并返回:[0],[1],[2,4,8,5,7,6,9,3]

第一个元素0是0,所以当你得到0时,它已经在当前循环中,所以转到下一个元素,即索引1处的元素,即4.一旦你在4,转到第四个元素,是7而不是8!

0 1 2 3 4 [0, 4, 2, 8, 7...

Don't ask why I took the time to do this.

EDIT: Totally working now

public class Main { public Main() { } public static void main(String[] args) { int array[] = {0, 4, 2, 8, 7, 9, 1, 6, 5, 3}; Main m = new Main(); int[][] cycles = m.getCycles(array); for (int i = 0; i < cycles.length; i++) { System.out.print("["); for (int j = 0; j < cycles[i].length; j++) { System.out.print(cycles[i][j]); if (j < cycles[i].length - 1) System.out.print(", "); } System.out.println("]"); } System.out.println("end debug"); } public int[][] getCycles(int[] array) { int[][] cycles = new int[array.length][array.length]; // initialize the cycles to all -1s, cuz they'll never be in the array for (int i = 0; i < cycles.length; i++) { for (int j = 0; j < cycles[i].length; j++) { cycles[i][j] = -1; } } int i = 0; do { int nextElement = array[i]; int j = 0; do { cycles[i][j] = nextElement; nextElement = array[nextElement]; j++; } while (!elementInArray(cycles[i], nextElement) && j < array.length); i++; } while (!arrayHasCycled(array, cycles) && i < array.length); cycles = removeNegativeOnes(cycles, i); for (i = 0; i < cycles.length; i++) { pushForward(cycles[i]); } return cycles; } public boolean elementInArray(int[] array, int element) { for (int i = 0; i < array.length; i++) { if( array[i] == element) return true; } return false; } public int[][] removeNegativeOnes(int[][] cycles, int numCycles) { int [][] newCycles = new int[numCycles][]; for (int i = 0; i < numCycles; i++) { int realLenOfCycle = indexOf(-1, cycles[i]); newCycles[i] = new int[realLenOfCycle]; for (int j = 0; j < newCycles[i].length; j++) { newCycles[i][j] = cycles[i][j]; } } return newCycles; } public int indexOf(int element, int[] array) { int index = -1; for (int i = 0; i < array.length; i++) { if (array[i] == element) return i; } return index; } public boolean arrayHasCycled(int[] array, int[][] cycles) { for (int i = 0; i < array.length; i++) { boolean cycleHasValue = false; for (int j = 0; j < cycles.length; j++) { for (int k = 0; k < cycles[j].length; k++) { if (cycles[j][k] == array[i]) cycleHasValue = true; } } if (!cycleHasValue) return false; } return true; } public void pushForward(int [] array) { int lastElement = array[array.length - 1]; for (int i = array.length - 1; i > 0; i--) { array[i] = array[i - 1]; } array[0] = lastElement; } }

Output:

[0] [1, 4, 7, 6] [2] [3, 8, 5, 9]

From what I understand, you're asked us to create a code which executes the following algorithm:

Create a one-dimensional array of integers, array For each element in that array, nextElement do the following: Create a new one-dimensional array, currCycle that will be added to a two-dimensional array, cycles. Set the first element of that array to nextElement. nextElement then becomes array[nextElement] If nextElement is already in currCycle, continue onto the next element of array Check if all the elements of array are in cycles, and if so, stop executing this algorithm. Finally, return the cycles as a two-dimensional array with the index that was being used instead of the element at that index, which is what the current array consists of. To accomplish this, just cyclically (in the normal sense) push each element of the array forward one index.

This doesn't follow your examples exactly, but I think you may have malformed your examples, for instance:

An example: [0, 4, 2, 8, 7, 9, 1, 6, 5, 3]

would be these cycles : [0] [4, 8, 5, 7, 6, 9, 3, 2] [1]

and return this: [0], [1], [2, 4, 8, 5, 7, 6, 9, 3]

The first element 0 is 0, so when you get 0, it's already in the current cycle, so go to the next element, the element at index 1, which is 4. Once you're at 4 go to the fourth element, which is 7 not 8!

0 1 2 3 4 [0, 4, 2, 8, 7...

更多推荐

本文发布于:2023-07-31 14:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1344947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   方法来   创建一个   排列   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!