#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; }