选择显示字体大小

一个实现排列和组合的javabean

出自:www.linuxaid.com.cn  
在我们编程时,经常要涉及到排列和组合的问题。那么在java中应该如何实现呢?其实这个问题首先是个算法的问题,明确了算法,用什么编程语言倒是显得不那么重要了。
一、全排列
所谓全排列,就是对n个对象,列出其所有可能的排列顺序。这个问题相对来说要简单一点。让我们先从最简单的情况着手,如果我们只有一个对象,该如何实现其全排列呢?答案很简单,现在我们只有一种排列。ok,现着我们试着引入第二个对象,首先我们需要知道,现在增加了几种全排列的可能,我们可以这么看,第n个对象的引入,对于n-1个对象的全排列的影响就是在原先的每一个全排列中的任一位置插入第n个对象,也就是说,listcount(n)=listcount(n-1)*n;而按照这个思路,对于任意n个对象的全排列,我们都可以从最简单的一个对象的全排列开始直到生成全部n个对象的全排列。
二、组合
所谓组合,就是从n个对象中取出任意m个对象的全部可能。这个问题可能要复杂一点。对于组合来说,对象的顺序是没有意义的,1、2、3和3、2、1是同一种可能。那么我们有必要人为地制定一个规则,我们可以设想给每一个对象编上连续的序号,而在我们的组合中对象必须是按其序号的顺序排列,这样我们就能有效地避免排列顺序对我们的影响。假定我们现在有六个对象,其序号是1、2、3、4、5、6。那么我们的第一种组合是1、2、3,而最后一种组合则是4、5、6。在此之间,我们经历了其它有可能出现的18种情况,对于这种算法我们很自然地会想到使用递归函数。首先我们先在我们的结果集中定义第一种可能是1、2、3,然后我们把当前位置定为最后一位,只要有可能,对于最后一位来说,它的最大值只能是6,在未到6之前,每增加一次就增加一种新的组合,如果最后一位已经是6,则我们将当前位置转入前一位,前一位的最大值是5,如果前一位还没到5,则将前一位加一,然后当前位置再度转入最后一位,现在最后一位的最小值是4,从4开始直到6,又形成了我们新的组合,这样,直到我们最终出现4、5、6这个组合时,函数退出。
下面是我们的java源程序:
mytest.java:
package customers;
public class mytest
{string[] mychar=new string[50];
int charcount;
int charlist;
public void setmytest()
//初始化
{charcount=0;
charlist=1;
}
public void insertchar(string thischar)
//增加新的字符串
{charcount++;
mychar[charcount]=thischar;
charlist*=charcount;
}
public string listallchar()
//列出全排列
{string[][] allchar=new string[charlist+1][charcount+1];
int i;
int j;
int z=1;
for (i=1;i<=charcount;i++)
{mycopy(addcharlist(i,allchar,z),allchar,charlist,charcount);
z*=i;
}
string listallchar=new string(\&quot;\&quot;);
for (i=1;i<=charlist;i++)
{for (j=1;j<=charcount;j++)
listallchar+=allchar[i][j]+\&quot; \&quot;;
listallchar=listallchar+\&quot;<br>\&quot;;
}
return listallchar;
}
public string[][] addcharlist(int i,string[][] allchar,int z)
//在i-1个对象的全排列中引入第i个对象
{int j;
int h=1;
int k;
string[][] tempallchar=new string[charlist+1][charcount+1];
for (k=1;k<=z;k++)
{for (j=1;j<=i;j++)
{mycopy(tempchar(j,allchar[k],mychar[i]),tempallchar[h],charcount);
h++;
}
}
return tempallchar;
}
public string[] tempchar(int i,string[] beginchar,string thischar)
//将新对象插入指定位置
{int j;
string[] tempbeginchar=new string[charcount+1];
mycopy(beginchar,tempbeginchar,charcount);
for (j=charcount;j>i;j--) tempbeginchar[j]=tempbeginchar[j-1];
tempbeginchar[i]=thischar;
return tempbeginchar;
}
public string selectsomechar(int select)
//列出其中取出select个对象的全部组合
{int selectcount=1;
int i;
for (i=select+1;i<=charcount;i++) selectcount=selectcount*i/(i-select);
string[][] selectchar=new string[selectcount+1][select+1];
int[][] selectint=new int[selectcount+1][select+1];
for (i=1;i<=select;i++)
{selectchar[1][i]=mychar[i];
selectint[1][i]=i;
}
int z=1;
addselect(selectchar,selectint,1,select,select);
int j;
string selectsomechar=new string(\&quot;\&quot;);
for (i=1;i<=selectcount;i++)
{for (j=1;j<=select;j++)
selectsomechar+=selectchar[i][j]+\&quot; \&quot;;
selectsomechar=selectsomechar+\&quot;<br>\&quot;;
}
return selectsomechar;
}
public void addselect(string[][] selectchar,int[][] selectint,int z,int position,int select)
//增加新的组合
{int i;
if (position==select)
{if (selectint[z][position]<charcount)
{z++;
mycopy(selectint[z-1],selectint[z],select);
selectint[z][select]++;
for (i=1;i<=select;i++) selectchar[z][i]=mychar[selectint[z][i]];
addselect(selectchar,selectint,z,position,select);
}
else
{position--;
addselect(selectchar,selectint,z,position,select);
}
}
else
{if (selectint[z][position]<charcount-select+position)
{selectint[z][position]++;
selectint[z][position+1]=selectint[z][position]+1;
position++;
addselect(selectchar,selectint,z,position,select);
}
else
{if (position==1)
{return;
}
else
{position--;
addselect(selectchar,selectint,z,position,select);
}
}
}
}
public void mycopy(string[][] str1,string[][] str2,int i,int j)
{int h;
int k;
for (h=1;h<=i;h++) for (k=1;k<=j;k++) str2[h][k]=str1[h][k];
}
public void mycopy(string[] str1,string[] str2,int i)
{int h;
for (h=1;h<=i;h++) str2[h]=str1[h];
}
public void mycopy(int[] str1,int[] str2,int i)
{int h;
for (h=1;h<=i;h++) str2[h]=str1[h];
}
}
现在我们可以在一个jsp程序中使用这个javabean:
<%@ page contenttype=\&quot;text/html;charset=gb2312\&quot; %>
<html>
<head>
<title>
排列和组合
</title>
</head>
<body>
<jsp:usebean id=\&quot;mytest\&quot; scope=\&quot;page\&quot; class=\&quot;customers.mytest\&quot; />
<%
mytest.setmytest();
mytest.insertchar(\&quot;ysy\&quot;);
mytest.insertchar(\&quot;dbf\&quot;);
mytest.insertchar(\&quot;cyy\&quot;);
mytest.insertchar(\&quot;ykf\&quot;);
mytest.insertchar(\&quot;sjj\&quot;);
mytest.insertchar(\&quot;yzds\&quot;);
out.print(mytest.listallchar());
out.print(mytest.selectsomechar(3));
%>
</body>
</html>
三、排列
所谓排列,就是从n个对象中取出m个对象的所有排列的可能。事实上,我们先生成这样的组合,然后对每一个组合进行全排列就可以了,有兴趣的读者可以自己试着实现这种功能


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons