GalaxyPilot —— D.S


        生命不熄,战斗不止
数据加载中……

求0-9的全排列,参考了网上的代码

public class qpl
{
 public static void main(String[] args)
 {
  char num[]={'0','1','2','3','4','5','6','7','8','9'};
  permutation(num,0,10);
 }
 public static void permutation(char num[] , int start , int len)
 {
  int i;
  char t;
  if (start < len - 1)
  {
   permutation(num, start+1,len);
   for (i=start+1;i<len;i++)
   {
    t=num[start];
    num[start]=num[i];
    num[i]=t;
    permutation(num,start+1,len);
    t=num[start];
    num[start]=num[i];
    num[i]=t;
   }
  }else
   System.out.println(num);
 }
}

posted on 2006-09-07 09:13 舵手 阅读(2549) 评论(2)  编辑  收藏

评论

# re: 求0-9的全排列,参考了网上的代码  回复  更多评论   

看这代码,想起了重构,同样的代码出现了两次
t=num[start];
num[start]=num[i];
num[i]=t;
没有考虑算法,只是对代码而论。重复是一切罪恶之源。
2006-09-09 10:09 | 布衣郎

# re: 求0-9的全排列,参考了网上的代码  回复  更多评论   

有些重复是必要的,人类一代一代的繁衍,不也是一种重复?
2006-09-15 17:17 | che

只有注册用户登录后才能发表评论。


网站导航: