题解 | 牛牛数数

牛牛数数

https://www.nowcoder.com/practice/03a3cc96fa4847b387bf58bb800d67cf

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

bool baohan4(int n){
    int a =0;
    while(n>0){
        if(n%10 == 4){
            return true;
        }
	  n/=10;
    }
    return false;
}

int main() {
    int  n;
    cin>>n;
    for(int i=1;i<=n;i++){
        if(!baohan4(i) && i%4 != 0)cout<<i<<endl;
    }
}
// 64 位输出请用 printf("%lld")

字符串方法:

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

int main() {
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        if (i % 4 == 0) continue;   // 是 4 的倍数,跳过

        string s = to_string(i);    // 转换为字符串
        if (s.find('4') != string::npos) continue; // 如果含有字符 '4',跳过

        cout << i << endl;          // 符合条件,输出
    }

    return 0;
}

全部评论

相关推荐

09-22 09:42
门头沟学院 Java
牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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