请问在1-2310中有多少个整数,不是2,3,5,7,11的倍数 请问在1-2310中有多少个整数,
测试图:
利用容斥原理
代码如下:
#include<iostream>
#define LL long long
using namespace std;
LL n,tot,ans=0;
int main()
{
cin>>n;
tot=(LL)n/2+n/3+n/5+n/7;
tot-=(LL)(n/6+n/10+n/14+n/15+n/21+n/35);
tot+=(LL)n/30+n/42+n/70+n/105;
tot-=(LL)(n/210);
ans=n-tot;
cout<<ans<<endl;
return 0;
}
希望对题主有所帮助,可以的话,帮忙点个采纳!
循环遍历,用求余为0判断是否整除
请问在1-2310中有多少个整数,不是2,3,5,7,11的倍数 请问在1-2310中有多少个整数
#include <iostream>
using namespace std;
int main()
{
int i,count=0;
for(i=1;i<=2310;i++)
{
if(i%2 != 0 && i%3!=0 && i%5!=0 && i%7!=0 && i%11!=0)
count++;
}
cout<<count<<endl;
return 0;
}