自除数 是指可以被它包含的每一位数整除的数。
例如,128 是一个 自除数 ,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。
自除数 不允许包含 0 。
给定两个整数 left 和 right ,返回一个列表,列表的元素是范围 [left, right] 内所有的 自除数 。
输入:left = 1, right = 22 输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
输入:left = 47, right = 85 输出:[48,55,66,77]
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans = new ArrayList<Integer>();
for (int i = left; i <= right; i++) {
if (check(i)) {
ans.add(i);
}
}
return ans;
}
public boolean check(int num) {
int temp = num;
while (temp > 0) {
int digit = temp % 10;
if (digit == 0 || num % digit != 0) {
return false;
}
temp /= 10;
}
return true;
}
}
import java.util.Scanner;
public class _11 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int head=sc.nextInt();
int last=sc.nextInt();
for(int i=head;i<=last;i++) {
if(check(i)) {
System.out.println(i);;
}
}
}
public static boolean check(int num) {
int temp=num;
while(temp>0) {
int n=temp%10;
if(n==0||num%n!=0) {
return false;
}
temp/=10;
}
return true;
}
}