LeetCode 728. 自除数 C语言解法

自除数 是指可以被它包含的每一位数除尽的数。

例如,128 是一个自除数,因为 128 % 1 == 0128 % 2 == 0128 % 8 == 0

还有,自除数不允许包含 0 。

给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数。

示例 1:

输入: 
上边界left = 1, 下边界right = 22
输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

注意:

    每个输入参数的边界满足 1 <= left <= right <= 10000

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* selfDividingNumbers(int left, int right, int* returnSize) 
{
    int res[10000]={0};
    int count=0;
    int temp,mod;
        for(int i=left;i<=right;i++)
        {
            temp=i;
            while(temp)
            {
                mod=temp%10;
                if(mod==0)
                {
                    break;
                }
                else
                {
                    if(i%mod==0)
                    {
                        temp/=10;
                    }
                    else
                    {
                        break;
                    }
                }
                
            }
            if(temp==0)
            {
                res[count]=i;
                count++;
            }
        }
        *returnSize=count;
        int *result=(int*)malloc(count*sizeof(int));
        for(int i=0;i<count;i++)
        {
            result[i]=res[i];
        }        
        return result;        
}

猜你喜欢

转载自blog.csdn.net/qq_27060423/article/details/82392586