Java置换
我正在尝试运行我的代码,以便它打印循环置换,尽管目前我只能让它执行第一个置换。它正确运行到我标记的点,但我看不出哪里出了问题。我认为它在while循环中没有中断,但我不确定。真的需要一些帮助Java置换,java,Java,我正在尝试运行我的代码,以便它打印循环置换,尽管目前我只能让它执行第一个置换。它正确运行到我标记的点,但我看不出哪里出了问题。我认为它在while循环中没有中断,但我不确定。真的需要一些帮助 package permutation; public class Permutation { static int DEFAULT = 100; public static void main(String[] args) { int n = DEFAULT; if (args.len
package permutation;
public class Permutation {
static int DEFAULT = 100;
public static void main(String[] args) {
int n = DEFAULT;
if (args.length > 0)
n = Integer.parseInt(args[0]);
int[] OA = new int[n];
for (int i = 0; i < n; i++)
OA[i] = i + 1;
System.out.println("The original array is:");
for (int i = 0; i < OA.length; i++)
System.out.print(OA[i] + " ");
System.out.println();
System.out.println("A permutation of the original array is:");
OA = generateRandomPermutation(n);
printArray(OA);
printPemutation(OA);
}
static int[] generateRandomPermutation(int n)// (a)
{
int[] A = new int[n];
for (int i = 0; i < n; i++)
A[i] = i + 1;
for (int i = 0; i < n; i++) {
int r = (int) (Math.random() * (n));
int swap = A[r];
A[r] = A[i];
A[i] = swap;
}
return A;
}
static void printArray(int A[]) {
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " ");
System.out.println();
}
static void printPemutation(int p[])// (b)
{
System.out
.println("The permutation is represented by the cyclic notation:");
int[] B = new int[p.length];
int m = 0;
while (m < p.length)// this is the point at which my code screws up
{
if (!check(B, m)) {
B = parenthesis(p, m);
printParenthesis(B);
m++;
} else
m++;
}// if not there are then repeat
}
static int[] parenthesis(int p[], int i) {
int[] B = new int[p.length];
for (int a = p[i], j = 0; a != B[0]; a = p[a - 1], j++) {
B[j] = a;
}
return B;
}
static void printParenthesis(int B[]) {
System.out.print("( ");
for (int i = 0; i < B.length && B[i] != 0; i++)
System.out.print(B[i] + " ");
System.out.print(")");
}
static boolean check(int B[], int m) {
int i = 0;
boolean a = false;
while (i < B.length || !a) {
if ((ispresent(m, B, i))){
a = true;
break;
}
else
i++;
}
return a;
}
static boolean ispresent(int m, int B[], int i) {
return m == B[i] && m < B.length;
}
}
包置换;
公共类置换{
静态int默认值=100;
公共静态void main(字符串[]args){
int n=默认值;
如果(args.length>0)
n=整数.parseInt(args[0]);
int[]OA=新的int[n];
对于(int i=0;i
除此之外,您应该在check(B,p[m])中勾选p[m],而不是m:
静态无效打印置换(int p[]):
while(m
然后
静态布尔检查(int B[],int m){
int i=0;
而(i
这在某种程度上更符合您的要求,但我担心并非总是如此……请帮我们一个忙,通过自动压头运行您的代码。你已经在完全相同的缩进级别上嵌套了ifs等,这使得破译逻辑变得不必要的困难。家庭作业,请这样标记。家庭作业是一个元标记,因此不鼓励。我只想让它转移到下一个循环排列,比如51432,表示为(521)(43)。然而目前它只是印刷品(521)。check方法遍历(521)并查看不在其中的第一个数字是什么,然后开始打印第二个括号,或者这就是想法。然而,我在如何让它移动以开始打印下一个括号的想法上走到了死胡同。实现提示我现在得到原始数组是:1 2 3 4原始数组的排列是:2 3 1 4该排列由循环符号表示:(2 3 1)(2 3 1)(3 1 2)(1 2 3 3)(4)抱歉,我无法完全修复您的代码。我想你在排列函数中也有一些错误。也许您应该首先尝试为一个类创建有效的排列:例如,(12),(13),(14),(23),(24),(34)尝试调试它似乎认为错误在最后一行的第三行。在ispresent方法中。关于如何扫描打印周期以找到下一步,还有其他想法吗?还是我的想法不对?
while (m < p.length){
if (!check(B, p[m])) {
B = parenthesis(p, m);
printParenthesis(B);
}
m++;
}
static boolean check(int B[], int m) {
int i = 0;
while (i < B.length) {
if (m == B[i]) {
return true;
}
i++;
}
return false;
}