奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

java語言

java算法實現排列組合的方法介紹

時間:2025-03-08 03:51:41 java語言 我要投稿
  • 相關推薦

java算法實現排列組合的方法介紹

  一.利用二進制狀態法求排列組合,此種方法比較容易懂,但是運行效率不高,小數據排列組合可以使用代碼如下:

  import java.util.Arrays;

  //利用二進制算法進行全排列

  //count1:170187

  //count2:291656

  public class test {

  public static void main(String[] args) {

  long start=System.currentTimeMillis();

  count2();

  long end=System.currentTimeMillis();

  System.out.println(end-start);

  }

  private static void count2(){

  int[] num=new int []{1,2,3,4,5,6,7,8,9};

  for(int i=1;i<Math.pow(9, 9);i++){

  String str=Integer.toString(i,9);

  int sz=str.length();

  for(int j=0;j<9-sz;j++){

  str="0"+str;

  }

  char[] temp=str.toCharArray();

  Arrays.sort(temp);

  String gl=new String(temp);

  if(!gl.equals("012345678")){

  continue;

  }

  String result="";

  for(int m=0;m<str.length();m++){

  result+=num[Integer.parseInt(str.charAt(m)+"")];

  }

  System.out.println(result);

  }

  }

  public static void count1(){

  int[] num=new int []{1,2,3,4,5,6,7,8,9};

  int[] ss=new int []{0,1,2,3,4,5,6,7,8};

  int[] temp=new int[9];

  while(temp[0]<9){

  temp[temp.length-1]++;

  for(int i=temp.length-1;i>0;i--){

  if(temp[i]==9){

  temp[i]=0;

  temp[i-1]++;

  }

  }

  int []tt=temp.clone();

  Arrays.sort(tt);

  if(!Arrays.equals(tt,ss)){

  continue;

  }

  String result="";

  for(int i=0;i<num.length;i++){

  result+=num[temp[i]];

  }

  System.out.println(result);

  }

  }

  }

  二.用遞歸的思想來求排列跟組合,代碼量比較大代碼如下:

  package practice;

  import java.util.ArrayList;

  import java.util.List;

  public class Test1 {

  /**

  * @param args

  */

  public static void main(String[] args) {

  // TODO Auto-generated method stub

  Object[] tmp={1,2,3,4,5,6};

  // ArrayList

  rs=RandomC(tmp);

  ArrayList

  rs=cmn(tmp,3);

  for(int i=0;i<rs.size();i++)

  {

  // System.out.print(i+"=");

  for(int j=0;j<rs.get(i).length;j++)

  {

  System.out.print(rs.get(i)[j]+",");

  }

  System.out.println();

  }

  }

  // 求一個數組的任意組合

  static ArrayList

  RandomC(Object[] source)

  {

  ArrayList

  result=new ArrayList

  ();

  if(source.length==1)

  {

  result.add(source);

  }

  else

  {

  Object[] psource=new Object[source.length-1];

  for(int i=0;i<psource.length;i++)

  {

  psource[i]=source[i];

  }

  result=RandomC(psource);

  int len=result.size();//fn組合的長度

  result.add((new Object[]{source[source.length-1]}));

  for(int i=0;i<len;i++)

  {

  Object[] tmp=new Object[result.get(i).length+1];

  for(int j=0;j<tmp.length-1;j++)

  {

  tmp[j]=result.get(i)[j];

  }

  tmp[tmp.length-1]=source[source.length-1];

  result.add(tmp);

  }

  }

  return result;

  }

  static ArrayList

  cmn(Object[] source,int n)

  {

  ArrayList

  result=new ArrayList

  ();

  if(n==1)

  {

  for(int i=0;i<source.length;i++)

  {

  result.add(new Object[]{source[i]});

  }

  }

  else if(source.length==n)

  {

  result.add(source);

  }

  else

  {

  Object[] psource=new Object[source.length-1];

  for(int i=0;i<psource.length;i++)

  {

  psource[i]=source[i];

  }

  result=cmn(psource,n);

  ArrayList

  tmp=cmn(psource,n-1);

  for(int i=0;i<tmp.size();i++)

  {

  Object[] rs=new Object[n];

  for(int j=0;j<n-1;j++)

  {

  rs[j]=tmp.get(i)[j];

  }

  rs[n-1]=source[source.length-1];

  result.add(rs);

  }

  }

  return result;

  }

  }

  三.利用動態規劃的思想求排列和組合 代碼如下:

  package Acm;

  //強大的求組合數

  public class MainApp {

  public static void main(String[] args) {

  int[] num=new int[]{1,2,3,4,5};

  String str="";

  //求3個數的組合個數

  // count(0,str,num,3);

  // 求1-n個數的組合個數

  count1(0,str,num);

  }

  private static void count1(int i, String str, int[] num) {

  if(i==num.length){

  System.out.println(str);

  return;

  }

  count1(i+1,str,num);

  count1(i+1,str+num[i]+",",num);

  }

  private static void count(int i, String str, int[] num,int n) {

  if(n==0){

  System.out.println(str);

  return;

  }

  if(i==num.length){

  return;

  }

  count(i+1,str+num[i]+",",num,n-1);

  count(i+1,str,num,n);

  }

  }

  下面是求排列代碼如下:

  package Acm;

  //求排列,求各種排列或組合后排列

  import java.util.Arrays;

  import java.util.Scanner;

  public class Demo19 {

  private static boolean f[];

  public static void main(String[] args) {

  Scanner sc=new Scanner(System.in);

  int sz=sc.nextInt();

  for(int i=0;i<sz;i++){

  int sum=sc.nextInt();

  f=new boolean[sum];

  Arrays.fill(f, true);

  int[] num=new int[sum];

  for(int j=0;j<sum;j++){

  num[j]=j+1;

  }

  int nn=sc.nextInt();

  String str="";

  count(num,str,nn);

  }

  }

  /**

  *

  * @param num 表示要排列的數組

  * @param str 以排列好的字符串

  * @param nn 剩下需要排列的個數,如果需要全排列,則nn為數組長度

  */

  private static void count(int[] num, String str, int nn) {

  if(nn==0){

  System.out.println(str);

  return;

  }

  for(int i=0;i<num.length;i++){

  if(!f[i]){

  continue;

  }

  f[i]=false;

  count(num,str+num[i],nn-1);

  f[i]=true;

  }

  }

  }

【java算法實現排列組合的方法介紹】相關文章:

權重隨機算法的java實現08-13

冒泡排序算法原理及JAVA實現代碼方法10-16

Java基于余弦方法實現的計算相似度算法示例09-03

java通用組合算法如何實現09-12

JAVA簡單選擇排序算法及實現10-02

KMP算法的C#實現方法08-29

如何實現java漢諾塔遞歸算法09-20

JAVA實現生成GUID的方法06-02

Java實現多線程的方法11-10

主站蜘蛛池模板: 长海县| 监利县| 闸北区| 文安县| 虞城县| 望都县| 东乡县| 阳新县| 庄浪县| 土默特左旗| 会宁县| 海盐县| 竹溪县| 德兴市| 曲阜市| 惠来县| 双柏县| 乌鲁木齐市| 赤峰市| 黄浦区| 项城市| 茶陵县| 晋江市| 禹州市| 贵溪市| 邹城市| 平定县| 玉门市| 汝城县| 新民市| 墨脱县| 荥经县| 凯里市| 襄樊市| 关岭| 长武县| 文昌市| 登封市| 东乌珠穆沁旗| 永济市| 金沙县|