Give you an integer num and return the number of digits in num that can divide num.
If nums % val == 0 is satisfied, the integer val is considered to be divisible by nums .
Example 1:
Input: num = 7
Output: 1
Explanation: 7 is divisible by itself, so the answer is 1.
Example 2:
Input: num = 121
Output: 2
Explanation: 121 Divisible by 1 but not divisible by 2. Since 1 occurs twice, 2 is returned.
Example 3:
Input: num = 1248
Output: 4
Explanation: 1248 It is divisible by every digit in it, so the answer is 4.
Tips:
1 <= num <= 10^9
The digits of num do not contain 0
#include<stdio.h>
int countDigits(int num);
int main()
{
int num;
scanf("%d",&num);
printf("num=%d\n",num);
printf("%d 次",countDigits(num));
}
int countDigits(int num){
int numCopy = num, count = 0,array[10] = {
0};
if(numCopy<10)
if(numCopy%numCopy == 0)
count++;
if(numCopy>10)
{
while (numCopy){
array[numCopy%10]++;
if(num%(numCopy%10) == 0){
count++;
printf("array = %d %d\n",numCopy%10,num%(array[numCopy%10]));
}
else
count = count;
numCopy /= 10;
}
}
return count;
}
//另一种方式
int countDigits(int num) {
int tmp;
int nums[10]={
};
for(tmp = num;tmp!=0;tmp/=10)
nums[tmp%10]++;
tmp=0;
for(int temp=0;temp<10;temp++)
if(nums[temp]&&num%temp==0)
//nums[temp]不等于 0 成立
tmp+=nums[temp];
return tmp;
}