您的位置:首页 > 其它

分治与递归(一)----全排列问题

2005-07-11 01:11 330 查看
//产生全排列,perm1(list,k,m)产生list[k...m]的全排列
import javax.swing.*;
public class Perm
{
static String output = "";
public static void swap(int [] list, int k, int m)
{
int temp;
temp = list[k];
list[k] = list[m];
list[m] = temp;
}
public static void perm1(int [] list, int k, int m))//求全排列
{
if(k == m)
{
for(int i = 0; i < m; i++)//单元素序列
output += list[i] + " ";
output += "/n";
}
else//多元素序列,递归产生排列
for(int i = k; i < m; i++)
{
swap(list,k,i);
perm1(list, k + 1, m);
swap(list,k,i);
}
}
public static void main(String args[])
{
String a,b;
int list[],n;
a = JOptionPane.showInputDialog("Input N");
n = Integer.parseInt(a);
list = new int
;
for(int i = 0; i < n; i++)
{
b = JOptionPane.showInputDialog("Input the data");
list[i] = Integer.parseInt(b);
}
perm1(list, 0, n);
JOptionPane.showMessageDialog(null,output,"The result",JOptionPane.INFORMATION_MESSAGE);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: