Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 算法研究1-排列显示
ql213





发贴: 6
积分: 0
于 2006-03-22 21:36 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

package MathMethod; //package name should begin with a lowercase character by convention
/**
* Title: 排列问题递归算法
* Description: 利用递归方法求解对象数组list[0:maxSize-1]的子数组list[k:m]的全排列
* Copyright (c) 2005
* Company: math
* @author ql213
*/
public class arrangeArithmetic { //class name should begin with an uppercase character by convention
static int size; //要排序的元素个数
static int Anum=0;
public arrangeArithmetic(int i,int j)
{
if(i>j)
{
System.out.println("Please insure that your input[i,j]:i<=j!");
return;
}
this.size=Math.abs(j-i)+1;
}
//交换对象数组的两个元素
public static void swap(Object []a,int i, int j)
{
Object temp=a[i];
a[i]=a[j];
a[j]=temp;
}
public static void perm(Object []list,int k,int m) throws Exception
{
//产生list[k:m]的所有排列
if(k==m)
{
//得到一种排列
Anum++;
System.out.print("Arrange: ");
for(int i=m-size+1;i<=m;i++)
{
System.out.print(list[i]);
}
System.out.println(" ");
}
else
{
//按照前面的递归思想产生排列
for(int i=k;i<=m;i++)
{
swap(list,k,i);
perm(list,k+1,m);//递归求解
swap(list,k,i);
}
}
}
public int ArrangeNum()
{
return Anum;//返回排列个数
}
}

{ Please use [code ] tag and check "Disable Smileys" for code listing. }
---------------------------------------------
测试结果:
Arrange: 1234
Arrange: 1243
Arrange: 1324
Arrange: 1342
Arrange: 1432
Arrange: 1423
Arrange: 2134
Arrange: 2143
Arrange: 2314
Arrange: 2341
Arrange: 2431
Arrange: 2413
Arrange: 3214
Arrange: 3241
Arrange: 3124
Arrange: 3142
Arrange: 3412
Arrange: 3421
Arrange: 4231
Arrange: 4213
Arrange: 4321
Arrange: 4312
Arrange: 4132
Arrange: 4123
The total Arrange number is: 24


why edited on 2006-03-27 01:24


话题树型展开
人气 标题 作者 字数 发贴时间
5420 算法研究1-排列显示 ql213 1878 2006-03-22 21:36

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923