打印出所有每一位都与其他位不重复的自然数
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; } }
相关文章推荐
- 左神算法 不重复打印排序数组中相加和为给定值的所有二元组
- 程序员面试题目总结--数组(一)【递归求数组所有元素和、用一个for循环打印出一个二维数组、用递归判断数组是否是递增、有序数组中删除重复元素】
- 若只有4KB内存可用,该如何打印数组中所有重复的元素
- 不重复打印排序数组中相加和为给定值的所有三元组
- 给一个字符串(包含重复字符),打印它的所有可能的组合。
- 打印一个含有重复元素数组的所有子集
- java 循环问题 大全 求2~100a之内的素数。求水仙花数:(1~1000)。有1、2、3、4四个数字,输出所有能组成的互不相同且无重复的三位数。打印空心菱形
- 从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数
- 9.10扩展性与存储限制(三)——若只有4KB内存可用,该如何打印数组中所有重复的元素
- 打印数组中所有重复的元素
- 一个无序自然数数列,给定一个数M,选取自然数数列中两个数,其中他们的和是M,打印所有这样的自然数对
- 判断101-200之间有多少个素数,并输出所有素数。素数为一个大于1的自然数,除了1和它本身外,不能被其他自然数整除。
- 从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数
- 一个整数列,除了一个数字重复2次,所有其他数字都重复4次。O(n)时间O(1)空间找到那个只出现2次的。
- C语言之结构体 现有36名教师,每个教师包括教工号,姓名,工资共三项基本信息,请用C语言编程,从键盘上输入每一位教师的基本信息,并打印输出工资最高者的工号,姓名工资和所有教师的平均工资
- java 集合 从类似如下的文本文件中读取出所有的姓名,并打印出重复的 * 姓名和重复的次数,并按重复次数排序:
- 不重复打印排序数组中相加和为给定值的所有二元三元组
- 不重复打印排序数组中相加和为给定值的所有二元组和三元组
- 打印出大小为n的数组(可能有重复元素)里所有可能的组合
- 面试题: 已知一个含有n个不同元素的集合,要求打印其所有具有k个元素的子集(不允许有重复的)