华为机试 HJ55 题解 | #挑7#

挑7

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

题目比较简单,从1~n遍历,依次遍历第i个数是否满足以下两个条件中的一个,如果满足其中一个则认为整数i与7相关,计数器加1,代码如下:

#include <iostream>
#include <string>
using namespace std;

int GetRel7Num(int n) {
    string str;
    int count = 0;
    for (int i = 1; i <= n; i++) {
        if (i % 7 == 0) {
            count++;
            continue;
        }
        str = std::to_string(i);
        if (str.find("7") != string::npos) {
            count++;
        }
    }
    return count;
}

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        cout << GetRel7Num(n) << endl;
    }
}

全部评论

相关推荐

06-25 16:25
梧州学院 Java
愿汐_:项目介绍那么长,然而你做了啥就一句话?
点赞 评论 收藏
分享
在等offer的火锅...:我去履历这么好,都找不到工作吗?
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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