网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
08月22日漏签0天
荆州职业技术学院...吧 关注:1,901贴子:7,725
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 22回复贴,共1页
<<返回荆州职业技...吧
>0< 加载中...

java经典算法1

  • 只看楼主
  • 收藏

  • 回复
  • 独白的PP
  • 小有美名
    5
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
算法程序题: 该公司笔试题就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;
}
}


  • 贪吃的小米虫0
  • 远近闻名
    10
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
妈呀、、、这还是人做的么?啦啦啦啦啦啦啦挖坟了啦啦啦啦啦


2025-08-22 03:11:38
广告
不感兴趣
开通SVIP免广告
  • sn876506943
  • 颇具盛名
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
笔试靠算法?纯手写?


  • sn876506943
  • 颇具盛名
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
要我写不会写这么多,不会超过二十行代码,数据量不大,直接全排列,不满足情况,就给PASS,就六个数,完全可以。。。。


  • 遥尘远望
  • 赫赫有名
    13
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
0.0 看着好多。。。其实我一点也不懂... ――天已亮,人已散。天已亮,奔课堂。天已亮……


  • CoCo丶流年
  • 颇具盛名
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
你以为公司面试会考这个? 这个不是学校考试好吗 。 孩子, 你太年轻。


  • W_未央
  • 声名远扬
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
要背下来么?!翻书可以么?


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 22回复贴,共1页
<<返回荆州职业技...吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示