?? fac2_4.java
字號:
//本程序取自王曉東編著“算法分析與設(shè)計”第 21 頁,例2-4
//排列問題的遞歸解法
class MyMath{
public MyMath(){
}
static void swap(double[] a,int i,int j)
{
double temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;}
}
public class Fac2_4{
static int j=0;
public static void perm(double[] list ,int k,int m)
{ //產(chǎn)生lisi[k:m]的所有排列
if(k==m)
{//單元素序列
j++;
for(int i=0;i<=m;i++)
System.out.print(list[i]+" ");
System.out.println("排列序號"+j);
}
else
// 多元素序列,遞歸產(chǎn)生排序
for(int i=k;i<=m;i++)
{
MyMath.swap(list,k,i);
perm(list,k+1,m);
MyMath.swap(list,k,i);
}
}
public static void main(String args[])
{
double a[]={9.0,78.8,23.6,2.9,56.78};
int x=a.length-1;
int y=0;
System.out.println("輸出排列結(jié)果");
perm(a,y,x);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -