n位数字的全排列共有n!种。
本排列只对字符型数字排列进行输出,输出的是字符型数字。这种问题一般都需要用递归的方法。
java代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class test { static int k= 0 ; public static void main(string[] args) { int a[]={ 1 , 2 , 3 , 4 , 5 }; permutations(a, 0 , 4 ); } public static void permutations( int []a, int m, int n){ if (m==n){ k++; system.out.print(k+ "个:" ); for ( int i= 0 ;i<=n;i++){ system.out.print(a[i]); } system.out.println(); } else { for ( int i=m;i<=n;i++){ int temp=a[m]; a[m]=a[i]; a[i]=temp; permutations(a,m+ 1 ,n); temp=a[m]; a[m]=a[i]; a[i]=temp; } } } |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/y999666/article/details/50777127