1.自除數(shù)
自除數(shù) 是指可以被它包含的每一位數(shù)除盡的數(shù)。
例如,128 是一個(gè)自除數(shù),因?yàn)?128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。
還有,自除數(shù)不允許包含 0 。
給定上邊界和下邊界數(shù)字,輸出一個(gè)列表,列表的元素是邊界(含邊界)內(nèi)所有的自除數(shù)。
示例 1:
輸入:
上邊界left = 1, 下邊界right = 22
輸出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
注意:
每個(gè)輸入?yún)?shù)的邊界滿足 1 <= left <= right <= 10000。
class Solution {
public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> res;
for( int i = left ; i <= right ; i++)
{
if(isornot(i))
res.push_back(i);
}
return res;
}
bool isornot( int num )
{
int temp = num ;
int last ;
while( temp )
{
if( temp % 10 == 0) return false;
if( num % ( temp % 10 ) ) return false;
temp /= 10;
}
return true ;
}
};