跪求,裸求.....大神正解....一到十,十个数....

threeswordmin9 2011-06-08 04:25:18
有{1,2,3,4,5,6,7,8,9,10}十个数字,要求相邻的两个数不能差的绝对值不能等于1,一共有多少种排列组合
...全文
126 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
threeswordmin9 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zqfddqr 的回复:]
呃 发错了

Java code

public class Traversal {

static long thesum = 0;

public static void main(String[] args) {
int buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

perm(buf,……
[/Quote]
执行了好久
凡员外 2011-06-09
  • 打赏
  • 举报
回复
算法了
zqfddqr 2011-06-08
  • 打赏
  • 举报
回复

_479301 0913825746
_479302 0913826475
_479303 0913847526
_479304 0913846257
_479305 0913846275
thesum is :479306

zqfddqr 2011-06-08
  • 打赏
  • 举报
回复
呃 发错了

public class Traversal {

static long thesum = 0;

public static void main(String[] args) {
int buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

perm(buf, 0, buf.length - 1);
System.out.println("thesum is :" + thesum);
}

public static void perm(int[] buf, int start, int end) {
if (start == end) {//当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
for (int i = 1; i <= end; i++) {
if (((buf[i] - buf[i - 1]) == 1) || ((buf[i] - buf[i - 1]) == -1)) {
return;
}
}
System.out.print("_" + thesum + " ");
for (int i = 0; i <= end; i++) {
System.out.print(buf[i]);
}
thesum++;
System.out.println();
} else {//多个字母全排列
for (int i = start; i <= end; i++) {
int temp = buf[start];//交换数组第一个元素与后续的元素
buf[start] = buf[i];
buf[i] = temp;

perm(buf, start + 1, end);//后续元素递归全排列

temp = buf[start];//将交换后的数组还原
buf[start] = buf[i];
buf[i] = temp;
}
}
}
}

部分代码来自 http://blog.csdn.net/justinavril/archive/2008/08/02/2758636.aspx
zqfddqr 2011-06-08
  • 打赏
  • 举报
回复
写个遍历的

public class Traversal {

static long thesum = 0;

public static void main(String[] args) {
int buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

perm(buf, 0, buf.length - 1);
System.out.println("thesum is :" + thesum);
}

public static void perm(int[] buf, int start, int end) {
if (start == end) {//当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
for (int i = 1; i <= end; i++) {
if (((buf[i] - buf[i - 1]) << 1 >> 1) == 1) {

return;
}
}
System.out.print("_" + thesum + " ");
for (int i = 0; i <= end; i++) {
System.out.print(buf[i]);
}
thesum++;
System.out.println();
} else {//多个字母全排列
for (int i = start; i <= end; i++) {
int temp = buf[start];//交换数组第一个元素与后续的元素
buf[start] = buf[i];
buf[i] = temp;

perm(buf, start + 1, end);//后续元素递归全排列

temp = buf[start];//将交换后的数组还原
buf[start] = buf[i];
buf[i] = temp;
}
}
}
}


部分代码来自 http://blog.csdn.net/justinavril/archive/2008/08/02/2758636.aspx
mrt_chen 2011-06-08
  • 打赏
  • 举报
回复

13,097

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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