lahand 2016-01-28 02:45 采纳率: 33.3%
浏览 6635
已采纳

JAVA 生成 用0到9这十个数字 所有的排列组合(0不能再第一个)

用 0到9 生成 十位数的所有排列组合,数字0不能在第一个,这个生成的十位数,
不能有重复的数字。

  • 写回答

4条回答

  • tony4geek 2016-01-28 03:18
    关注
     public static void main(String[] args) {
            String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            permutation(str, 0, str.length);
        }
    
        static void swap(String[] str, int start, int end) {
            String tmep = str[start];
            str[start] = str[end];
            str[end] = tmep;
        }
    
        static void permutation(String[] str, int start, int end) {
    
            if (start == end - 1) {
                for (int i = 0; i < end; i++) {
                    System.out.print(str[i]);
                }
                System.out.println();
            } else {
    
                for (int i = start; i < end; i++) {
                    if (i == 0 && str[0].equals("0"))
                        continue;
                    swap(str, start, i);
                    permutation(str, start + 1, end);
    
                    swap(str, start, i);
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料