一道笔试题求指导

Shellphon 2012-03-14 09:25:32
现在有1,2,3,3,4,6几个数字,请打印所有排列,规则:3和6不能搞基(相邻),4不能当小三(位于第三),请用java实现之。
好久没写,忘了咋写了,一开始就卡在排列打印上,囧。求知道
...全文
104 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2012-03-15
  • 打赏
  • 举报
回复
你这应该只有全排吧,没有排除掉36相邻的以及4在第三位的情况
Shellphon 2012-03-15
  • 打赏
  • 举报
回复
回三楼,是的,其他情况只是小case,终于有人来了
Shellphon 2012-03-14
  • 打赏
  • 举报
回复
好吧,csdn论坛也不一定积极,还是自己找。、、、
public class Test {
public static void main(String[] args) {
System.out.println("Input a string: ");
String text = new java.util.Scanner(System.in).next();
permutate(text);
}

static void permutate(String text) {
permutate(text.toCharArray(), 0, text.length());
}

static void permutate(char[] array, int current, int length) {
if (current == length) {
print(array);
return;
}
for (int i = current; i < length; ++i) {
swap(array, current, i);
permutate(array, current + 1, length);
swap(array, current, i);
}
}

static void print(char[] array) {
for (char c : array)
System.out.print(c);
System.out.println();
}

static void swap(char[] array, int first, int second) {
char temp = array[first];
array[first] = array[second];
array[second] = temp;
}

}

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧