A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

package test;

public class Test1
{
        public static void main(String[] args)
        {
                for(int a=1;a<=6;a++)
                {
                System.out.println(a);
                for(int b=a+1;b<=6;b++)
                {
                System.out.print(a);
                System.out.print(b);
                System.out.println();
                for(int c=b+1;c<=6;c++)
                {
                System.out.print(a);
                System.out.print(b);
                System.out.print(c);
                System.out.println();
                for(int d=c+1;d<=6;d++)
                {
                System.out.print(a);
                System.out.print(b);
                System.out.print(c);
                System.out.print(d);
                System.out.println();
                for(int e=d+1;d<=6;d++)
                {
                System.out.print(a);
                System.out.print(b);
                System.out.print(c);
                System.out.print(d);
                System.out.print(e);
                System.out.println();
                }
                }
                }
                }
                }
                System.out.print("123456");
        }
}




12 个回复

倒序浏览
楼主的方法是不是有点多呀,你可以用迭代的方法,代码会简洁很多。
回复 使用道具 举报
对呀要不你试试看能写出来吗好像还有个递归什么的不太会哦
回复 使用道具 举报
  1. public class PeiLie
  2. {  
  3.     public static void main(String[] args)
  4.         {  
  5.         String str = "abcd";  
  6.         peiLie("",str);  
  7.     }  
  8.     public static void peiLie(String a,String b)
  9.         {  
  10.         if(a.length()=="abcd".length())
  11.                 {  
  12.           System.out.println(a);  
  13.         }
  14.                 else
  15.                 {   
  16.            for(int i = 0; i< b.length(); i++)
  17.                         {  
  18.                String tempa = new String(a);  
  19.                String tempb = new String(b);  
  20.                peiLie(tempa+tempb.charAt(i),new StringBuilder(tempb).deleteCharAt(i).toString());  
  21.            }  
  22.         }  
  23.     }  
  24. }
复制代码
楼主运行结果怎么是这样呢?


字母全排列.jpg (98.35 KB, 下载次数: 25)

字母全排列.jpg

楼主的运行结果这样吗.jpg (31.75 KB, 下载次数: 19)

楼主的运行结果这样吗.jpg

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
public class AllCombination
{
        private static void showAllCombination(Object[] objs, int lastIndex)
        {
                if(lastIndex < 0)
                {
                        for(Object obj:objs)
                        {
                                System.out.print(obj);
                        }
                        System.out.println();
                        return;
                }
                Object temp = null;
                for(int i = 0; i<=lastIndex; ++i)
                {
                        temp = objs[i];
                        objs[i] = objs[lastIndex];
                        objs[lastIndex] = temp;
                        showAllCombination(objs, lastIndex - 1);
                        temp = objs[i];
                        objs[i] = objs[lastIndex];
                        objs[lastIndex] = temp;
                }
        }
        public  static void showAllCombination(Object[] objs)
        {
                showAllCombination(objs, objs.length - 1);
        }
        public  static void main(String[] args)
        {
                Character[] array = {'1','2','3','4','5','6'};
                showAllCombination(array);
        }
}

我这个程序是输出六个字符的全排列情况,由于情况很多,建议把array改为4个来运行看看结果。
楼主的意思是要全排列呢?还是所有组合(可以不包含所有字符)?
回复 使用道具 举报
什么情况,我的for语句里面
for(Object obj:objs)
包含了字符:o
结果网站当作一个表情了啊!!!!!!!
{:soso_e120:}
回复 使用道具 举报

什么情况,我的for语句里面
for(Object :  objbjs)
包含了字符连续字符: o
结果网站当作一个表情了啊!!!!!!!
回复 使用道具 举报
这个是数组为 {'1','2','3','4'}的时候的输出结果

QQ截图20121201125720.png (5.9 KB, 下载次数: 25)

QQ截图20121201125720.png
回复 使用道具 举报
方建平 发表于 2012-12-1 12:56
什么情况,我的for语句里面
for(Object :  objbjs)
包含了字符连续字符: o

高级模式,右边有一个禁用表情,你勾一下就行了
回复 使用道具 举报
tfy 中级黑马 2012-12-1 13:06:44
10#
王震阳 发表于 2012-12-1 12:33
楼主运行结果怎么是这样呢?

对的哦就是这样
回复 使用道具 举报
王震阳 发表于 2012-12-1 12:33
楼主运行结果怎么是这样呢?

{:soso_e141:}楼主要的是六个字符的排列还是1到6个字符的排列额?
回复 使用道具 举报
冯海霞 发表于 2012-12-1 13:09
楼主要的是六个字符的排列还是1到6个字符的排列额?

不管要的哪个,貌似都少吧?
回复 使用道具 举报
tfy 发表于 2012-12-1 13:06
对的哦就是这样

怎么还有重复的。

怎么还有23466.jpg (14.92 KB, 下载次数: 26)

怎么还有23466.jpg
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马