注意
如果同时满足规则1,2,那么cnt3++,此时cnt1&&cnt2不再++。
#include<bits/stdc++.h>
using namespace std;
int n, cnt1, cnt2, cnt3;
bool solve(int x, int D)
{
while (x)
{
int t = x % 10;
if (t == D)
{
return true;
}
x /= 10;
}
return false;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
if ((i % 3 == 0 || solve(i, 3)) && (i % 5 == 0 || solve(i, 5)))
{
cnt3++;
}
else if(i % 3 == 0 || solve(i, 3))
{
cnt1++;
}
else if (i % 5 == 0 || solve(i, 5))
{
cnt2++;
}
}
cout << cnt1 << endl << cnt2 << endl << cnt3 << endl;
return 0;
}