Title description
The self-divisor is the number that can be divided 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, the self-divisor is not allowed to contain 0.
Given the upper and lower boundary numbers, output a list, the elements of the list are all self-divisors within the boundary (including the boundary).
Example
Input:
upper boundary left = 1, lower boundary right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
note
The boundary of each input parameter satisfies 1 <= left <= right <= 10000.
Problem solving ideas
Judging the self-division function: According to the meaning of the question, if it is not composed of 0 and can be divisible by its own number, it is the self-division. Here you only need to extract each digit to see if it can be divisible
Main function: allocate space, if self-divisor appears, put it in a new space
Code
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
bool isSelfdivid(int num){
int init=num;
while(num){
int n=num%10;
if(n==0)return false;
else if(init%n!=0)return false;
num/=10;
}return true;
}
int* selfDividingNumbers(int left, int right, int* returnSize){
*returnSize=0;
int *re =calloc(right-left+1,sizeof(int));
int j=0;
for(int i=left;i<=right;i++){
int temp=i;
if(isSelfdivid(temp)==true){
re[j]=temp;
j++;
++(*returnSize);
}
}return re;
}