1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. java算法字符組合排序

        時(shí)間:2020-08-15 16:16:46 JAVA認(rèn)證 我要投稿

        java算法字符組合排序

          為幫助同學(xué)們學(xué)好java算法,yjbys小編為大家分享最新java算法——字符組合排序試題和答案如下:

          題目:

          用1、2、2、3、4、5這六個(gè)數(shù)字,用java寫一個(gè)main函數(shù),打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"與"5"不能相連。

          不是特別難的題目,暴力算和用圖論算(深度遍歷)都可以,結(jié)果是198.圖論的'話就是構(gòu)造無向圖,然后深度優(yōu)先遞歸。

          package com.graphic;

          import java.util.Iterator;

          import java.util.TreeSet;

          public class CharSequence {

          private String[] c = {"1","2","2","3","4","5"};

          private int n = c.length;

          private boolean[] visited = new boolean[n];

          private int[][] g = new int[n][n];

          private TreeSetts = new TreeSet();

          private String result = "";

          public CharSequence(){

          for(int i=0; i

          for(int j=0; j

          if(i == j) g[i][j] = 0;

          else g[i][j] = 1;

          }

          }

          g[3][5] = 0;

          g[5][3] = 0;

          }

          public void depthFirst(int index){

          visited[index] = true;

          result += c[index];

          if(result.length() == n){

          ts.add(result);

          result = result.substring(0,result.length()-1);

          visited[index] = false;

          }

          else{

          for(int i=0; i

          if(!visited[i] && g[index][i] == 1){

          depthFirst(i);

          }else continue;

          }

          result = result.substring(0,result.length()-1);

          visited[index] = false;

          }

          }

          public void graphicGet(){

          for(int i=0; i

          depthFirst(i);

          }

          int count = 0;

          System.out.print("圖論的結(jié)果:");

          Iteratorit = ts.iterator();

          while(it.hasNext()){

          String tmp = it.next();

          if(tmp.contains("35")) continue;

          if(tmp.contains("53")) continue;

          if(tmp.charAt(3) == '4') continue;

          System.out.println(tmp);

          count++;

          }

          System.out.println("共計(jì):"+count+"個(gè)");

          }

          public void bruteForce(){

          System.out.println("暴力搜的結(jié)果:");

          int count = 0;

          for(int i = 122345; i<543222; i++){

          String tmp = ""+i;

          if(tmp.charAt(3) == '4') continue;

          if(tmp.contains("35")) continue;

          if(tmp.contains("53")) continue;

          if(tmp.contains("5") && tmp.contains("4") && tmp.contains("3") && tmp.contains("1"))

          {

          int index = tmp.indexOf("2");

          if(index == -1) continue;

          if(index == tmp.length()-1) continue;

          if(tmp.substring(index+1).contains("2")){

          System.out.println(tmp);

          count++;

          }

          }

          }

          System.out.print("共計(jì):"+count+"個(gè)");

          }

          public void recrusive(){

          }

          public static void main(String[] args) {

          CharSequence cs = new CharSequence();

          //圖論的方法

          cs.graphicGet();

          //暴力搜索

          cs.bruteForce();

          }

          }

        【java算法字符組合排序】相關(guān)文章:

        java排序算法大全06-07

        java五種排序算法匯總09-17

        PHP中strnatcmp()函數(shù)“自然排序算法”進(jìn)行字符串比較用法分析11-16

        常見的php排序算法11-19

        關(guān)于Java的字符轉(zhuǎn)換10-07

        java字符串轉(zhuǎn)化整型問題05-12

        關(guān)于Java入門的字符型數(shù)據(jù)10-08

        Java編程中如何實(shí)現(xiàn)中文排序09-27

        Java認(rèn)證基礎(chǔ)知識(shí):java字符串轉(zhuǎn)化整型問題09-30

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>