有关算法的程序题

简介:

今天上网的时候看到这样一道程序题,做完后觉得很经典,写出来与大家分享。

算法程序题: 该公司笔试题就1个,要求在10分钟内作完。 题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。

 

 
  1. static int[] bits = new int[] { 1, 2, 3, 4, 5 }; 
  2.  
  3. /** 
  4.  * @param args 
  5.  */ 
  6. public static void main(String[] args) { 
  7. sort("", bits); 
  8.  
  9. private static void sort(String prefix, int[] a) { 
  10. if (a.length == 1) { 
  11. System.out.println(prefix + a[0]); 
  12.  
  13. for (int i = 0; i < a.length; i++) { 
  14. sort(prefix + a[i], copy(a, i)); 
  15.  
  16. private static int[] copy(int[] a,int index){ 
  17. int[] b = new int[a.length-1]; 
  18. System.arraycopy(a, 0, b, 0, index); 
  19. System.arraycopy(a, index+1, b, index, a.length-index-1); 
  20. return b; 

基本思路: 1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是 最后对这6个数字的排列组合结果集。 2 显然这个结果集还未达到题目的要求。从以下几个方面考虑: 1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。 2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果 3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。 采用二维数组定义图结构,最后的代码是:

 

 
  1. import java.util.Iterator; 
  2. import java.util.TreeSet; 
  3.  
  4. public class TestQuestion { 
  5.  
  6. private String[] b = new String[]{"1", "2", "2", "3", "4", "5"}; 
  7. private int n = b.length; 
  8. private boolean[] visited = new boolean[n]; 
  9. private int[][] a = new int[n][n]; 
  10. private String result = ""
  11. private TreeSet set = new TreeSet(); 
  12.  
  13. public static void main(String[] args) { 
  14. new TestQuestion().start(); 
  15.  
  16. private void start() { 
  17.  
  18. // Initial the map a[][] 
  19. for (int i = 0; i < n; i++) { 
  20. for (int j = 0; j < n; j++) { 
  21. if (i == j) { 
  22. a[i][j] = 0; 
  23. } else { 
  24.     a[i][j] = 1; 
  25.  
  26. // 3 and 5 can not be the neighbor. 
  27. a[3][5] = 0; 
  28. a[5][3] = 0; 
  29.  
  30. // Begin to depth search. 
  31. for (int i = 0; i < n; i++) { 
  32.     this.depthFirstSearch(i); 
  33.  
  34. // Print result treeset. 
  35. Iterator it = set.iterator(); 
  36. while (it.hasNext()) { 
  37. String string = (String) it.next(); 
  38. // "4" can not be the third position. 
  39. if (string.indexOf("4") != 2) { 
  40. System.out.println(string); 
  41.  
  42. private void depthFirstSearch(int startIndex) { 
  43. visited[startIndex] = true; 
  44. resultresult = result + b[startIndex]; 
  45. if (result.length() == n) { 
  46. // Filt the duplicate value. 
  47. set.add(result); 
  48. for(int j = 0; j < n; j++) { 
  49. if (a[startIndex][j] == 1 && visited[j] == false) { 
  50. depthFirstSearch(j); 
  51. } else { 
  52. continue; 
  53.  
  54. // restore the result value and visited value after listing a node. 
  55.     resultresult = result.substring(0, result.length() -1); 
  56.     visited[startIndex] = false; 

怎么样,很有收获吧,哈哈


本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/416697,如需转载请自行联系原作者


相关文章
|
7月前
|
算法
第2章 算法——程序的灵魂
第2章 算法——程序的灵魂
37 0
|
3月前
|
算法
写一段圆弧插补算法程序
写一段圆弧插补算法程序
36 0
|
3月前
|
存储 算法 搜索推荐
Java常见算法-算法与程序、公式、数据结构的区别
算法、程序、公式和数据结构是计算机科学中的基本概念,它们之间有着密切的联系,但各自有着独特的含义和作用。
|
3月前
|
缓存 算法 JavaScript
提高Java程序性能!了解可达性分析算法、强软弱虚引用和三色标记GC的过程,避免不可达对象阻碍程序性能!
提高Java程序性能!了解可达性分析算法、强软弱虚引用和三色标记GC的过程,避免不可达对象阻碍程序性能!
|
5月前
|
算法 编译器 C语言
算法小白的心得笔记:分清楚执行程序和动态链接库的编译方式。
-fPIC 选项:这个选项告诉编译器生成位置无关代码(Position Independent Code)。这种代码同样可以在内存的任何位置执行,因为它使用的是相对地址而不是绝对地址。这对于动态库是必要的,因为动态库在被加载时,其在内存中的位置是不确定的。
27 0
|
6月前
|
算法
100个经典c算法 | 程序源码
100个经典c算法 | 程序源码
34 0
|
9月前
|
机器学习/深度学习 算法 数据挖掘
【MATLAB第3期】源码分享#数学建模常用算法程序整理
【MATLAB第3期】源码分享#数学建模常用算法程序整理
|
10月前
|
算法 计算机视觉 异构计算
基于FPGA的图像sobel边缘提取算法实现,包含testbench和matlab验证程序
基于FPGA的图像sobel边缘提取算法实现,包含testbench和matlab验证程序
191 0
|
10月前
|
算法
雪花算法程序实现及史上最全解析
雪花算法实现及介绍 (生产可以直接使用)
|
11月前
|
编解码 算法 异构计算
m基于FPGA的LDPC最小和译码算法verilog实现,包括testbench和matlab辅助验证程序
m基于FPGA的LDPC最小和译码算法verilog实现,包括testbench和matlab辅助验证程序
211 0