打印出所有每一位都与其他位不重复的自然数

 1 package com.zl.jvm;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class TestSet {
 6 
 7     
 8     public static void main(String[] args) {
 9 
10         //ArrayList<String> s2 = new ArrayList<>();
11 
12         char[] a = { '0', '1', '2', '3', '4','5','6','7','8','9' };
13         //先插入1位不重复的排出0
14         ArrayList<String> first  = new ArrayList<>();
15         first.add("1");
16         first.add("2");
17         first.add("3");
18         first.add("4");
19         first.add("5");
20         first.add("6");
21         first.add("7");
22         first.add("8");
23         first.add("9");
24 
25     /*    for (int i = 0; i < a.length; i++) {
26             char temp1 = a[i];
27 
28             if ('0' == temp1) {
29                 continue;
30             }
31             for (int j = 0; j < a.length; j++) {
32                 char temp2 = a[j];
33 
34                 if (temp1 == temp2) {
35                     continue;
36                 }
37 
38                 s2.add("" + temp1 + temp2);
39 
40             }
41         }*/
42         
43         //计算出0-9组成2位长度 每一位不重复自然数 例:12 13
44         //11这种 算重复
45         ArrayList<String> s = wei(first,a,2); 
46         System.out.println(s.size());
47 
48     }
49     
50     public static ArrayList<String> wei(ArrayList<String>s,char[] a,int count) {
51         
52         ArrayList<String> s3 = new ArrayList<>();
53         
54         if(count>1) {
55             count-=1;
56             for (int i = 0; i < s.size(); i++) {
57                 StringBuilder sb = new StringBuilder();
58                 String temp = s.get(i);
59                 sb.append(temp);
60                 char[] chars2 = temp.toCharArray();
61                 for (int j = 0; j < a.length; j++) {
62                     if(check(chars2,a[j])) {
63                         s3.add(sb.toString() + a[j]);
64                     }
65                     
66                 }
67 
68             }
69             return wei(s3,a,count);
70         }
71         return s;
72     }
73     
74 
75     //判断是否每一位和要组合的数字是否重复
76     private static boolean check( char[] ch,char temp) {
77         for (int z = 0; z < ch.length; z++) {
78             if (ch[z] == temp) {
79                 return false;
80             }
81         }
82         return true;
83     }
84 }

 

posted on 2014-03-27 00:20  c3tc3tc3t  阅读(179)  评论(0编辑  收藏  举报