分治与递归(一)----全排列问题
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);
}
}
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);
}
}
相关文章推荐
- 递归和分治策略之排列问题
- 第二章 递归与分治策略(排列的字典序问题)
- 【算法设计与分析】递归与分治----2.4 排列问题
- 递归与分治——排列问题
- [置顶] 递归与分治策略-2.9.2线性时间选择(取中位数的中位数基准)(第k小问题)
- 递归和分治思想解全排列问题
- 递归之排列问题
- 分治与递归——标准二维表问题
- 使用递归-分治方法解决汉诺塔问题
- 递归求排列问题
- 递归解决问题的几种类型的排列(二、完整的组合安排)
- 七分钟笔记——递归之排列问题
- 八皇后问题使用排列的方法递归解决
- 分治与递归法:全排列问题
- 递归求解几类排列组合问题(一、类循环组合排列)
- 递归求解几类排列组合问题(四、普通选择性组合排列)
- 用递归写排列组合问题
- C/C++ 排列组合问题(递归)
- 递归求全排列问题学习
- 递归实现字母的排列问题