#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
long long int a , b ,c,r ;
while(cin >> a >> b )
{
r = 0 ;
for (int i = a ; i <= b ; i+=1)
{
c = 0;
if (i == 2 or i == 3 or i == 5 or i == 7 or i == 11)
r = r + 1;
if ( i%2 != 0 and i%3 !=0 and sqrt(i) != int(sqrt(i)) and i%11 !=0 and i%7 !=0 and i%5 !=0)
{
for (int k = 2 ; k <= sqrt(i) ; k+=1)
{
if ( i%k == 0)
c = 1;
}
if (c == 0)
r = r +1;
}
}
cout << r << endl;
}
return 0;
}
小弟是個初學者
打了這題打很久
希望有人可以給個建議程式要如何打起,指點一下
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long long int a , b ,c,r ;
while(cin >> a >> b )
{
r = 0 ;
for (int i = a ; i <= b ; i+=1)
{
c = 0;
if (i == 2 or i == 3 or i == 5 or i == 7 or i == 11)
r = r + 1;
if ( i%2 != 0 and i%3 !=0 and sqrt(i) != int(sqrt(i)) and i%11 !=0 and i%7 !=0 and i%5 !=0)
{
for (int k = 2 ; k <= sqrt(i) ; k+=1)
{
if ( i%k == 0)
c = 1;
}
if (c == 0)
r = r +1;
}
}
cout << r << endl;
}
return 0;
}
小弟是個初學者
打了這題打很久
希望有人可以給個建議程式要如何打起,指點一下
用for迴圈寫以下
for(int k=iA; k<=iB; ++k)
{
for( int i=2 ; i<(int)sqrt(k)+1 ; ++i )
{
if((k%i) == 0)
{
iFlag = 1;
break;
}
}
if (iFlag !=1 )
{
iAns +=1;
iFlag = 0;
}
else
iFlag =0;
}
最後再寫個if iA = 1
那麼iAns就檢一
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
long long int a , b ,c,r ;
while(cin >> a >> b )
{
r = 0 ;
for (int i = a ; i <= b ; i+=1)
{
c = 0;
if (i == 2 or i == 3 or i == 5 or i == 7 or i == 11)
r = r + 1;
if ( i%2 != 0 and i%3 !=0 and sqrt(i) != int(sqrt(i)) and i%11 !=0 and i%7 !=0 and i%5 !=0)
{
for (int k = 2 ; k <= sqrt(i) ; k+=1)
{
if ( i%k == 0)
c = 1;
}
if (c == 0)
r = r +1;
}
}
cout << r << endl;
}
return 0;
}
小弟是個初學者
打了這題打很久
希望有人可以給個建議程式要如何打起,指點一下