您的位置:首页 > 其它

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

2014-03-27 00:20 162 查看
package com.zl.jvm;

import java.util.ArrayList;

public class TestSet {

public static void main(String[] args) {

//ArrayList<String> s2 = new ArrayList<>();

char[] a = { '0', '1', '2', '3', '4','5','6','7','8','9' };
//先插入1位不重复的排出0
ArrayList<String> first  = new ArrayList<>();
first.add("1");
first.add("2");
first.add("3");
first.add("4");
first.add("5");
first.add("6");
first.add("7");
first.add("8");
first.add("9");

/*    for (int i = 0; i < a.length; i++) {
char temp1 = a[i];

if ('0' == temp1) {
continue;
}
for (int j = 0; j < a.length; j++) {
char temp2 = a[j];

if (temp1 == temp2) {
continue;
}

s2.add("" + temp1 + temp2);

}
}*/

//计算出0-9组成2位长度 每一位不重复自然数 例:12 13
//11这种 算重复
ArrayList<String> s = wei(first,a,2);
System.out.println(s.size());

}

public static ArrayList<String> wei(ArrayList<String>s,char[] a,int count) {

ArrayList<String> s3 = new ArrayList<>();

if(count>1) {
count-=1;
for (int i = 0; i < s.size(); i++) {
StringBuilder sb = new StringBuilder();
String temp = s.get(i);
sb.append(temp);
char[] chars2 = temp.toCharArray();
for (int j = 0; j < a.length; j++) {
if(check(chars2,a[j])) {
s3.add(sb.toString() + a[j]);
}

}

}
return wei(s3,a,count);
}
return s;
}

//判断是否每一位和要组合的数字是否重复
private static boolean check( char[] ch,char temp) {
for (int z = 0; z < ch.length; z++) {
if (ch[z] == temp) {
return false;
}
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐