第二道题交卷之后想到的,不知道对不对
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int res = 0;
String n = sc.next();
int m = sc.nextInt();
List<Character> characterList = new LinkedList<>();
for(int i=0;i<n.length();i++){
characterList.add(n.charAt(i));
}
List<Integer> list = new ArrayList<>();
for(int i=0;i<characterList.size();i++){
StringBuilder temp = new StringBuilder();
temp.append(characterList.get(0));
char a = characterList.remove(0);
dfs(temp,characterList,list);
characterList.add(a);
}
for(int i:list){
if(i%m==0){
res++;
}
}
System.out.println(res);
}
private static void dfs(StringBuilder temp, List<Character> characterList,List<Integer> list ) {
if(characterList.isEmpty()){
list.add(Integer.valueOf(temp.toString()));
return;
}
for(int i=0;i<characterList.size();i++){
temp.append(characterList.get(0));
char a = characterList.remove(0);
dfs(temp,characterList,list);
temp.deleteCharAt(temp.length()-1);
characterList.add(a);
}
}
}
全部评论
(0) 回帖