LeetCode每日一题——2520. Count the Digits That Divide a Number

文章目录

一、题目

2520. Count the Digits That Divide a Number

Given an integer num, return the number of digits in num that divide num.

An integer val divides nums if nums % val == 0.

Example 1:

Input: num = 7

Output: 1

Explanation: 7 divides itself, hence the answer is 1.

Example 2:

Input: num = 121

Output: 2

Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.

Example 3:

Input: num = 1248

Output: 4

Explanation: 1248 is divisible by all of its digits, hence the answer is 4.

Constraints:

1 <= num <= 109

num does not contain 0 as one of its digits.

二、题解

cpp 复制代码
class Solution {
public:
    int countDigits(int num) {
        int tmp = num;
        int res = 0;
        while(tmp){
            int mod = tmp % 10;
            if(num % mod == 0) res++;
            tmp = tmp / 10;
        }
        return res;
    }
};
相关推荐
沐雪轻挽萤10 分钟前
6. C++17新特性-编译期 if 语句 (if constexpr)
开发语言·c++
水云桐程序员13 分钟前
C语言编程基础,输入与输出
c语言·开发语言·算法
ZPC821015 分钟前
MoveIt Servo 与自己编写的 Action Server 通信
人工智能·算法·机器人
jllllyuz17 分钟前
采用核函数的极限学习机(KELM)MATLAB实现
算法
apcipot_rain26 分钟前
【天梯赛】2026天梯赛模拟赛——题解
开发语言·c++·算法·蓝桥杯·天梯赛
-To be number.wan28 分钟前
重新认识一下“私有继承”
c++·学习
江奖蒋犟1 小时前
【C++】红黑树
开发语言·c++
lclin_20201 小时前
【大恒相机】C++ 设备枚举+打开关闭+启停采集(基础入门)
c++·工业相机·大恒相机·galaxysdk
.柒宇.1 小时前
力扣hot100之最大子数组和(Java版)
数据结构·算法·leetcode
黎阳之光1 小时前
非视距·自愈·广覆盖|黎阳之光1.4&5.8GHz宽带自愈网无线基站,重构工业级无线通信
大数据·人工智能·算法·安全·数字孪生