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

//产生全排列,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[n];
  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);
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值