从26个字母中取出三个字母并排列,打印出所有情况

解决方案 »

  1.   

    排列就是有顺序的了 对吧 abc 和 cba 不能算一个 有时间帮你写一个
      

  2.   

    我理解的应该是不能重复的3个字母按顺序排列吧。
    下午比较无聊, 按照上午的思路写了个。package suanfa;import java.util.Arrays;public class Sort {

    public static void main(String[] args){

    new Sort().ergodic(3);
    }
    /**
     * 将方法重载下, 只需要传入需要的长度
     * @param length 
     */
    private void ergodic(int length) {
    ergodic('a', length, new char[0]);
    }


    /**
     * @param first 第一个字符
     * @param left 剩余需要的串长
     * @param str 已经拼接的字符
     */
    private void ergodic(char first, int left, char[] str) {
    char next = (char)(first+1); //下一个
    //已经是最后一个了,将其输出
    if(left == 1){
    //直接输出
    char[] newStr = Arrays.copyOf(str, str.length+1);
    newStr[str.length] = first;
    System.out.println(newStr);
    if(first == 'z')//最后一个 结束了
    return;
    //还没到最后 继续递归
    this.ergodic(next, left, str);
    }
    else{
    if(first == 'z')//最后一个 结束了
    return;
    //用第一个字符
    char[] newStr = Arrays.copyOf(str, str.length+1);
    newStr[str.length] = first;
    this.ergodic(next, left - 1, newStr); //递归
    //不用第一个字符
    this.ergodic(next, left , str); 
    }
    }
    }
      

  3.   

    嘿嘿 我下午也写了一个import java.util.ArrayList;/**
     *
     *
     * permutation and combination
     *
     * @author Administrator
     */
    public class Pac {    Pac(String str) {
            this.str = str;
        }
        String str;//源字符串。  
        String state;//c or a 状态
        /*
         * 组合数 
         */ 
       
        public void permutation(int c) {
            state="a";
            alc = new ArrayList<String>();
            ala = new ArrayList<String>();
            c(str, c);
            for (String s : alc) {
                a(s, c);
            }
            for (String s : ala) {
                System.out.println(s);
            }
            System.out.println("共计" + ala.size() + "个 组合");
        }
        ArrayList<String> ala = new ArrayList<String>();//返回用的al    void a(String str, int c) {
            filla(str, c, "");
        }    void filla(String str, int c, String pre) {
            if (c <= 0) {
                System.out.println("选择的个数太少必须大于0当前是" + c);
                return;
            }
            if (c == 1) {
                ala.add(pre + str.charAt(0));
                return;
            }
            for (int i = 0; i  < str.length(); i++) {
                filla(str.substring(0, i) + str.substring(i + 1, str.length()), c - 1, pre + str.charAt(i));//正常
            }    }    /*
         * 排列数
         */
        public void combination(int c) {
            state="c";
            alc = new ArrayList<String>();
            c(str, c);
            for (String s : alc) {
                System.out.println(s);
            }
            System.out.println("共计" + alc.size() + "个 排列");
        }
        /*
         * 内部组合
         */    private ArrayList<String> c(String str, int c) {
            if (c > str.length()) {
                System.out.println("数组内部出错选择大于数组长度");
                return null;
            }
            fillc(str, c, "");
            return null;
        }
        ArrayList<String> alc = new ArrayList<String>();//返回用的al    void fillc(String str, int c, String pre) {
            if (c <= 0) {
                System.out.println("选择的个数太少必须大于0当前是" + c);
                return;
            }
            if (c == 1) {
                for (char s : str.toCharArray()) {
                    alc.add(pre + s);
                }
                return;
            }
            fillc(str.substring(1, str.length()), c - 1, pre + str.charAt(0));//正常
            //重填
            if (c <= str.length() - 1) {
                fillc(str.substring(1, str.length()), c, pre);
            }
        }    public static void main(String[] args) {
            Pac p = new Pac("123456789");
            p.combination(3);
            p.permutation(3);    }
    }
      

  4.   

    Java里面有排列组合的算法吧。自己写的话,看起来代码都不是很Generic。
      

  5.   

    这个是个组合问题,你可以看下我的博客
    http://xieyan30.iteye.com/admin/blogs/1814117
    看看能不能帮助到你