小杨申请了一个保密柜,但是他忘记了密码。只记得密码都是数字,而且所有数字都是不
重复的。请你根据他记住的数字范围和密码的最小数字数量,帮他算下有哪些可能的组
合,规则如下:
1
、输出的组合都是从可选的数字范围中选取的,且不能重复;
2
、输出的密码数字要按照从小到大的顺序排列,密码组合需要按照字母顺序,从小到大的
顺序排序。
3
、输出的每一个组合的数字的数量要大于等于密码最小数字数量;
4
、如果可能的组合为空,则返回“
None
”
输入描述:
1
、输入的第一行是可能的密码数字列表,数字间以半角逗号分隔
2
、输入的第二行是密码最小数字数量
输出描述:
可能的密码组合,每种组合显示成一行,每个组合内部的数字以半角逗号分隔,从小到大
的顺序排列。
输出的组合间需要按照字典序排序。
比如:
2,3,4
放到
2,4
的前面
补充说明:
字典序是指按照单词出现在字典的顺序进行排序的方法,比如:
a
排在
b
前
a
排在
ab
前
ab
排在
ac
前
ac
排在
aca
前
public class GusPassword {
public static void main(String[] args) {
//处理数据
Scanner in=new Scanner(System.in);
String str = in.nextLine();
int len=in.nextInt();
String[] strArr=str.split(",");
List<Integer> charList=new ArrayList<>();
for (String s : strArr) {
charList.add(Integer.parseInt(s));
}
Collections.sort(charList);
if (charList.size()<len){
System.out.println("Node");
}else {
List<Integer> target=new ArrayList<>();
dfs(charList,target,len);
}
}
public static void dfs(List<Integer> list,List<Integer> resultList,int len){
if (resultList.size()>=len){
putValue(resultList);
}
for (int i=0;i<list.size();i++){
if (resultList.size()==0||list.get(i)>resultList.get(resultList.size()-1)){
//获取当前的值进入结果集
resultList.add(list.get(i));
//递归
dfs(list,resultList,len);
//将遍历之后的数据删除
resultList.remove(resultList.size()-1);
}
}
}
/**
* 输出需要的字符串
*/
public static void putValue(List<Integer> list){
String result="";
for (Integer integer : list) {
result += integer + ",";
}
result=result.substring(0,result.length()-1);
System.out.println(result);
}
}