Java && C#

          要學得東西很多,但我們的時間卻不是很多!
          數據加載中……
          Java經典算法集

          算法程序題:

          ??? 該公司筆試題就1個,要求在10分鐘內作完。

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



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

          /**
          ?* @param args
          ?*/
          public static void main(String[] args) {
          sort("", bits);
          }

          private static void sort(String prefix, int[] a) {
          if (a.length == 1) {
          System.out.println(prefix + a[0]);
          }

          for (int i = 0; i < a.length; i++) {
          sort(prefix + a[i], copy(a, i));
          }
          }

          private static int[] copy(int[] a,int index){
          int[] b = new int[a.length-1];
          System.arraycopy(a, 0, b, 0, index);
          System.arraycopy(a, index+1, b, index, a.length-index-1);
          return b;
          }


          **********************************************************************

          ? 基本思路:
          1 把問題歸結為圖結構的遍歷問題。實際上6個數字就是六個結點,把六個結點連接成無向連通圖,對于每一個結點求這個圖形的遍歷路徑,所有結點的遍歷路徑就是最后對這6個數字的排列組合結果集。
          2 顯然這個結果集還未達到題目的要求。從以下幾個方面考慮:
          ? 1. 3,5不能相連:實際要求這個連通圖的結點3,5之間不能連通, 可在構造圖結構時就滿足改條件,然后再遍歷圖。
          ? 2. 不能有重復: 考慮到有兩個2,明顯會存在重復結果,可以把結果集放在TreeSet中過濾重復結果
          ? 3. 4不能在第三位: 仍舊在結果集中去除滿足此條件的結果。

          采用二維數組定義圖結構,最后的代碼是:

          import java.util.Iterator;
          import java.util.TreeSet;

          public class TestQuestion {

          private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
          private int n = b.length;
          private boolean[] visited = new boolean[n];
          private int[][] a = new int[n][n];
          private String result = "";
          private TreeSet set = new TreeSet();

          public static void main(String[] args) {
          new TestQuestion().start();
          }

          private void start() {

          // Initial the map a[][]
          for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
          if (i == j) {
          a[i][j] = 0;
          } else {
          ??? a[i][j] = 1;
          }
          }
          }

          // 3 and 5 can not be the neighbor.
          a[3][5] = 0;
          a[5][3] = 0;

          // Begin to depth search.
          for (int i = 0; i < n; i++) {
          ??? this.depthFirstSearch(i);
          }

          // Print result treeset.
          Iterator it = set.iterator();
          while (it.hasNext()) {
          String string = (String) it.next();
          // "4" can not be the third position.
          if (string.indexOf("4") != 2) {
          System.out.println(string);
          }
          }
          }

          private void depthFirstSearch(int startIndex) {
          visited[startIndex] = true;
          result = result + b[startIndex];
          if (result.length() == n) {
          // Filt the duplicate value.
          set.add(result);
          }
          for(int j = 0; j < n; j++) {
          if (a[startIndex][j] == 1 && visited[j] == false) {
          depthFirstSearch(j);
          } else {
          continue;
          }
          }

          // restore the result value and visited value after listing a node.
          ??? result = result.substring(0, result.length() -1);
          ??? visited[startIndex] = false;
          }
          }

          ?

          posted on 2007-03-04 14:17 Bill111 閱讀(21397) 評論(21)  編輯  收藏

          評論

          # re: Java經典算法集 2007-03-25 08:27 圓規

          大家好好看看吧!加油哦!
            回復  更多評論    

          # re: Java經典算法集 2008-09-06 10:45 123

          好啊好啊
            回復  更多評論    

          # re: Java經典算法集 2008-10-22 20:11 cc-yuechuzhao

          我的算法,不用任何數據結構
          public class choose6 {
          public static void main(String[] args) {
          int t=0;
          int x;
          for (x = 122345; x < 543222; x++) {
          if ((x + "").indexOf('2') >= 0) {
          if ((x + "").indexOf('2', (x + "").indexOf('2')) >= 0) {
          if (((x + "").indexOf('3') >= 0)
          && ((x + "").indexOf('4') >= 0)
          && ((x + "").indexOf('5') >= 0)
          &&((x + "").indexOf("35")<0)
          &&((x + "").indexOf("53")<0)
          &&((x + "").indexOf('4')!=2)
          )
          ++t;
          System.out.println(x);
          }
          }
          }System.out.println(t);
          }
          }
            回復  更多評論    

          # re: Java經典算法集 2009-11-10 15:10 感應

          @cc-yuechuzhao
          錯的
            回復  更多評論    

          # re: Java經典算法集 2010-07-02 15:01 tt

          public static void main(String[] args) {
          for(int i=111111;i<555555;i++){
          String str=i+"";
          if(str.indexOf("35")==-1 && str.indexOf("53")==-1){

          if(!str.substring(2).startsWith("4")){
          System.out.println(str);
          }
          }
          }


          }
            回復  更多評論    

          # re: Java經典算法集[未登錄] 2012-07-25 14:15 過客

          冒泡排序
            回復  更多評論    

          # re: Java經典算法集 2013-01-22 06:18 Zi由的天空

          @感應
          怎么錯了
            回復  更多評論    

          # re: Java經典算法集 2013-03-01 15:10 adual

          我來猥瑣一把:
          for (int i = 122345; i < 543222; i++) {
          String sb = String.valueOf(i);
          String temp = sb.replaceFirst("1", "a");
          temp = temp.replaceFirst("2", "a");
          temp = temp.replaceFirst("2", "a");
          temp = temp.replaceFirst("3", "a");
          temp = temp.replaceFirst("4", "a");
          temp = temp.replaceFirst("5", "a");
          //數字的構成必須用指定的6位數
          if (! "aaaaaa".equalsIgnoreCase(temp)) {
          continue;
          }
          /*如果4在第三位則不計此數*/
          if (sb.indexOf("4") ==2) {
          continue;
          }
          /*判斷3和5不能在一起*/
          int flag = 0;
          if (sb.indexOf("35") > -1 || sb.indexOf("53") > -1) {
          flag ++;
          }
          if (flag > 0) {
          continue;
          }

          System.out.println(i);
          }
          }
            回復  更多評論    

          # re: Java經典算法集 2013-03-09 01:37 酷到你尖叫

          @adual 強!!
            回復  更多評論    

          # re: Java經典算法集 2013-05-14 17:59 wu0103356

          import java.util.Iterator;
          import java.util.TreeSet;

          public class 我的排序 {

          /**
          * @param args
          */
          public static void main(String[] args) {
          // TODO Auto-generated method stub
          int a[] = { 1, 2, 2, 3, 4, 5 };
          TreeSet<String> set = new TreeSet<String>();
          for (int i = 0; i < 6; i++) {
          for (int k = 0; k < 6; k++) {
          for (int l = 0; l < 6; l++) {
          for (int m = 0; m < 6; m++) {
          for (int n = 0; n < 6; n++) {
          for (int o = 0; o < 6; o++) {
          if (l == 4 || i == k || i == l || i == m
          || i == n || i == o || k == l || k == m
          || k == n || k == o || l == m || l == n
          || l == o || m == n || m == o || n == o) {
          continue;
          }
          String s = a[i] + "" + a[k] + "" + a[l] + ""
          + a[m] + "" + a[n] + "" + a[o];
          if(s.contains("35")||s.contains("53")){
          continue;
          }
          set.add(s);
          }
          }
          }
          }
          }
          }
          Iterator i = set.iterator();
          while (i.hasNext())
          System.out.println(i.next());
          System.out.println(set.size());
          }

          }
            回復  更多評論    

          # re: Java經典算法集 2013-06-09 11:22 番茄炒蛋

          @wu0103356
          這個排序太兇殘了,這么多排序。辛虧數組長度不長啊。
            回復  更多評論    

          # re: Java經典算法集 2013-07-29 15:26 好球哥哥

          import java.util.ArrayList;
          import java.util.HashSet;
          import java.util.List;
          import java.util.Set;

          //遞歸傳入字符數組,輸出有多少組合
          public class 排列組合 {
          public static void main(String arg[]) throws Exception {
          char[] c = new char[] {'1','2','2','3','4','5'};// 定義一個做實驗的數組

          List<char[]> l = new 排列組合().dg(c);// 計算出所有組合

          Set<String> set = new HashSet<String>();//開始去掉重復
          for (char[] ctemp : l) {
          String s = new String(ctemp);
          if(s.charAt(2)=='4'){
          continue;
          }
          if(s.indexOf("35")>=0){
          continue;
          }
          if(s.indexOf("53")>=0){
          continue;
          }
          set.add(s);
          }

          for(String s:set){//輸出
          System.out.println(s);
          }

          }

          //遞歸排序方法
          public List<char[]> dg(char[] targetArray) {
          if (targetArray.length == 2) {// 如果數組只有2個就不用遞歸了,直接輸出順序
          List<char[]> list2 = new ArrayList<char[]>();
          list2.add(new char[] { targetArray[0], targetArray[1] });
          list2.add(new char[] { targetArray[1], targetArray[0] });
          return list2;
          }

          List<char[]> returnList = new ArrayList<char[]>();//定義要返回的結果list
          for (Integer i = 0; i < targetArray.length; i++) {
          char temp = targetArray[i];
          char[] result = new char[targetArray.length - 1];
          for (Integer j = 0; j < targetArray.length; j++) {
          if (j < i) {
          result[j] = targetArray[j];
          } else if (j > i) {
          result[j-1] = targetArray[j];
          }
          }
          List<char[]> listaaa = charJiaList(temp, dg(result));
          for (char[] c1111 : listaaa) {
          returnList.add(c1111);
          }

          }
          return returnList;
          }

          //dg方法的輔助方法
          private List<char[]> charJiaList(char c, List<char[]> listchar) {
          List<char[]> listC = new ArrayList<char[]>();
          for (char[] charTemp : listchar) {
          char[] charTemp1 = new char[charTemp.length + 1];
          charTemp1[0] = c;
          for (Integer i = 1; i < charTemp1.length; i++) {
          charTemp1[i] = charTemp[i - 1];
          }
          listC.add(charTemp1);
          }
          return listC;
          }
          }
            回復  更多評論    

          # re: Java經典算法集[未登錄] 2013-09-26 02:13 john

          你改一下,把數字組合換成字母或者字符,難度就出來了,這樣下面的山寨將全部打倒,不過還是有可以不用數據結構的簡單方法,大家有興趣可以再來切磋下,呵呵
            回復  更多評論    

          # re: Java經典算法集[未登錄] 2013-09-26 02:15 john

          @wu0103356
          這個笨辦法,有點搞笑,太沒技術含量了
            回復  更多評論    

          # re: Java經典算法集[未登錄] 2013-09-26 02:16 john

          @adual
          你這個山寨了,如果換成其他字符,你就沒辦法,哈哈
            回復  更多評論    

          # re: Java經典算法集[未登錄] 2013-09-26 02:19 john

          @john
          哦,我仔細看了下,好像錯了
            回復  更多評論    

          # re: Java經典算法集 2013-10-17 17:10 迷迷

          public static void main(String[] args) {
          int[] bits = new int[] { 6, 7, 8, 9, 0 };
          for (int i = 122345; i <= 543221; i++) {
          boolean b = false;
          for (int j = 0; j < bits.length; j++) {

          if (("" + i).indexOf("" + bits[j]) >= 0) {
          b = true;
          break;
          }
          }
          if (b == false) {
          if (("" + i).substring(2, 3).equals("4") == false) {
          if (("" + i).indexOf("35") == -1) {
          System.out.println(i);
          }
          }
          }
          }

          }
            回復  更多評論    

          # re: Java經典算法集[未登錄] 2013-11-21 01:28 titan

          public class SoftTest {
          public static void main(String[] args)
          {
          for(int i=122345; i<=543221 ;i++ )
          {
          String s = String.valueOf(i);
          if(s.indexOf("2")>=0&&s.indexOf("3")>=0&&s.indexOf("4")>=0&&s.indexOf("5")>=0&& s.indexOf("4")!=2&&s.indexOf("1")>=0&&s.indexOf("35")==-1&&s.indexOf("53")==-1&&s.indexOf("2",s.indexOf("2")+1)>=0)
          {
          System.out.println(i);
          }

          }
          }
          }
            回復  更多評論    

          # re: Java經典算法集 2014-04-17 20:09 大額_skylar

          @迷迷
          錯了...有重復的數字,不能算是不同的排列,數字排列一個數字不能出現兩次
            回復  更多評論    

          # re: Java經典算法集 2014-04-24 20:11 scdxcc

          public class Test {
          public static void main(String[] args) {
          for (int i = 122345; i <= 543221; i++) {
          String s = String.valueOf(i);
          if ((s.charAt(2) != '4')
          && (Math.abs(s.indexOf("3") - s.indexOf("5")) > 1)
          && (s.indexOf("0") == -1) && (s.indexOf("6") == -1)
          && (s.indexOf("7") == -1) && (s.indexOf("8") == -1)
          && (s.indexOf("9") == -1) && find(s, '1') == 1
          && find(s, '2') == 2 && find(s, '3') == 1
          && find(s, '4') == 1 && find(s, '5') == 1) {
          System.out.println(i);
          }

          }
          }

          public static int find(String s, char d) {
          int count = 0;
          for (int i = 0; i < s.length(); i++) {
          if (s.charAt(i) == d) {
          count++;

          }

          }

          return count;
          }
          }
            回復  更多評論    

          # re: Java經典算法集[未登錄] 2014-06-24 16:45 牛牛

          @大額_skylar
          package niuce.common.arith;

          public class PlusSort {

          public static void main(String[] args) {
          getAllSerizes(new int[]{2,4,8,2,6});
          }

          public static int[] bubleSort(int[] nums){
          int temp=0;
          for(int i=nums.length-1;i>0;i--){
          for(int j=0;j<i;++j){
          if(nums[j+1]<nums[j]){
          temp=nums[j];
          nums[j]=nums[j+1];
          nums[j+1]=temp;
          }
          }
          }
          return nums;
          }

          public static void getAllSerizes(int[] nums){
          int[] zn=bubleSort(nums);
          String minNum="";
          String maxNum="";
          String flag="";
          for(int i=0;i<zn.length;i++){
          minNum+=zn[i];
          maxNum+=zn[zn.length-i-1];
          flag+="a";
          }
          int inMin=Integer.parseInt(minNum);
          int inMax=Integer.parseInt(maxNum);
          for(int i=inMin;i<=inMax;i++){
          String temp=Integer.toString(i);
          for(int j=0;j<zn.length;j++){
          temp=temp.replaceFirst(Integer.toString(zn[j]),"a");
          }
          if(flag.equalsIgnoreCase(temp)){
          System.out.println(i);
          }
          }
          }
          }
            回復  更多評論    

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 鄄城县| 德安县| 东港市| 成都市| 东方市| 开原市| 赣榆县| 上蔡县| 油尖旺区| 通道| 五常市| 定远县| 平邑县| 青州市| 鄂托克旗| 通州市| 浑源县| 靖边县| 新龙县| 繁昌县| 航空| 芦山县| 通江县| 抚松县| 香港 | 银川市| 阳东县| 大安市| 长沙市| 吉林市| 昭平县| 云龙县| 迁安市| 通河县| 孝义市| 昌乐县| 呼和浩特市| 中方县| 宿松县| 金坛市| 临西县|