A self-dividing number is a number that is divisible by every digit it contains.
For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
Also, a self-dividing number is not allowed to contain the digit zero.
Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.
Example 1:
Input:left = 1, right = 22Output:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
Note:
The boundaries of each input argument are 1 <= left <= right <= 10000.
自分数就是一个数能被它自身的每一位数字整除。只要循环判断从left到right中每一个数是不是自分数就好了。
注意数字中只要出现了零就肯定不是自分数。
···
class Solution {public: vectorselfDividingNumbers(int left, int right) {
vector res;
for(int i = left;i<=right;i++){
int tmp = i;
int temp;
bool flag;
flag = true;
while(tmp>0){
temp = tmp%10;
if(temp == 0|| i % temp != 0){
flag = false;
break;
}
tmp = tmp/10;
}
if(flag == true){
res.push_back(i);
}
}
return res;
}
};
···
网友评论