高手帮写下注释,越详细越好啊!!!

珠江的风 2013-05-12 08:12:07
/**
* Java经典算法集——如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,
* 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
*/
package com.test1;

import java.util.Set;
import java.util.TreeSet;

public class NumberSort {

public static Set<String> set = new TreeSet<String>();

public static void perm(char[] n, int beg, int end) {
if (beg == end) {
addNumber(String.valueOf(n));
} else {
for (int i = beg; i <= end; ++i) {
swap(n, beg, i);
perm(n, beg + 1, end);
swap(n, beg, i);
}
}
}

public static void swap(char[] n, int x, int y) {
if (x == y || n[x] == n[y]) {
return;
}
char temp = n[x];
n[x] = n[y];
n[y] = temp;
}

public static void addNumber(String str) {
if (str.charAt(2) == '4' || str.contains("35") || str.contains("53")) {
return;
}
set.add(str);
}

public static void main(String args[]) {
char[] number = new char[] { '1', '2', '2', '3', '4', '5' };
perm(number, 0, number.length - 1);
System.out.println("共有:"+set.size());
int cols = 10;
System.out.println("分别是");
for (String s : set) {
System.out.print(s + " ");
if (cols-- == 1) {
System.out.println();
cols = 10;
}
}
}
}
//小弟实在看不懂perm方法和swap方法是干什么用的,请高手重点注释这两个方法,小弟感激不尽
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
珠江的风 2013-05-17
  • 打赏
  • 举报
回复
貌似懂了,谢谢各位大神
anshiny 2013-05-14
  • 打赏
  • 举报
回复
   //枚举出n数组中从beg到end的全部排列
    public static void perm(char[] n, int beg, int end) {
        //如果beg和end相等,对应排列只有一种,结束。
    	if (beg == end) {
            addNumber(String.valueOf(n));
        } else {//如果beg和end不等。
            for (int i = beg; i <= end; ++i) {//枚举出beg位元素在各个位置上的排列情况
                swap(n, beg, i);//beg位元素与i位元素互换
                perm(n, beg + 1, end);//枚举出n数组中从beg+1到end的全部排列
                swap(n, beg, i);//beg位元素与i位元素互换,恢复原样
            }
        }
    }

    
   //在数组中交换下标是x和y的两个元素
    public static void swap(char[] n, int x, int y) {
    	//下标相同或内容相同,不交换
    	if (x == y || n[x] == n[y]) {        
            return;
        }
        //交换:
        char temp = n[x];
        n[x] = n[y];
        n[y] = temp;
    }
zgh_mnb 2013-05-14
  • 打赏
  • 举报
回复
俨然一个看不懂代码的。。。
confirmAname 2013-05-13
  • 打赏
  • 举报
回复
我想问问撸主这里的代码是从哪里来的?Java经典算法集?是纸质书还是网上的电子文档。似乎看起来不错。能不能发我一份,我也研究研究....
  • 打赏
  • 举报
回复
public static void perm(char[] n, int beg, int end) { // 如果最后一个位置也排列好了就调用判断条件 if (beg == end) { addNumber(String.valueOf(n)); } else { //对前面的位置进行排列 //从beg位置开始排直到end位置 for (int i = beg; i <= end; ++i) { swap(n, beg, i); perm(n, beg + 1, end); swap(n, beg, i); } } } public static void swap(char[] n, int x, int y) { //如果需要判断的位置一致就返回 if (x == y || n[x] == n[y]) { return; } //不一致就对x和y的位置进行调换 char temp = n[x]; n[x] = n[y]; n[y] = temp; }
fei1710 2013-05-12
  • 打赏
  • 举报
回复
for (int i = beg; i <= end; ++i) { swap(n, beg, i); perm(n, beg + 1, end); swap(n, beg, i); } 从6个里面拿一个放到第1个位置,因为原来位置里也有,所以要交换。 从剩下的5个里面拿一个放到第2个位置。 接下来就是从剩下4个里面选一个,最后就剩一个了,直接拿过来输出就好了。 输出后,再把原来的位置换回去。 这里的输出结果直接在原数组里通过交换得到的,看上去不是那么直观。

62,628

社区成员

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

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