题解 | #挑7#

挑7

https://www.nowcoder.com/practice/ba241b85371c409ea01ac0aa1a8d957b

#include <iostream>
using namespace std;
#include <cmath>
bool isSeven(int n)
{
    if (n % 7 == 0)
    {
        return true;
    }
    else
    {
        int x;
        for (int i = 1; i <= 5; i++)
        {
            int y = pow(10,i);
            x = n % y; 
            
            if (x % 7 == 0 && x != 0)
            {
                return true;
            }
            n = n - x;
            if(n <= 0){
                return false;
            }
        }
    }
    return false;
}
int main()
{
    int num;
    cin >> num;
    int cnt = 0;
    for (int i = 1; i <= num; i++)
    {
        if (isSeven(i))
        {
            //cout <<"i: " <<i << endl;
            cnt++;
        }
    }
    cout << cnt << endl;
}

通过求余计算,记得输出一下看看情况

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务