TJU1010
此題有多種做法,直接數也行(枚舉所有數然后判斷有多少質數);或者維護一個質數列表,輸入兩個數以后搜索有幾個質數。我用的是前者
#include<iostream>
#include<cmath>
using?namespace?std;
int?IsPrime(long?Num);
int?main()
{
????long?int?n,m,sum;
????while(cin>>n>>m)
????{
????????sum=0;
????????for(;n<=m;n++)
????????????if(IsPrime(n))?sum++;
????????cout<<sum<<endl;
????}
????return?0;
}
int?IsPrime(long?Num)
{
????if(Num<=2)?return?Num-1;
????if(Num%2?==?0)?return?0;
????int?s?=?(int)sqrt((double)Num);
????int?t=3;
????for(;t<=s;t+=2)
????????if(Num%t?==?0)?return?0;
????return?1;
}
#include<iostream>
#include<cmath>
using?namespace?std;
int?IsPrime(long?Num);
int?main()
{
????long?int?n,m,sum;
????while(cin>>n>>m)
????{
????????sum=0;
????????for(;n<=m;n++)
????????????if(IsPrime(n))?sum++;
????????cout<<sum<<endl;
????}
????return?0;
}
int?IsPrime(long?Num)
{
????if(Num<=2)?return?Num-1;
????if(Num%2?==?0)?return?0;
????int?s?=?(int)sqrt((double)Num);
????int?t=3;
????for(;t<=s;t+=2)
????????if(Num%t?==?0)?return?0;
????return?1;
}
轉載于:https://www.cnblogs.com/FancyMouse/articles/219655.html
總結
- 上一篇: livewriter写Blog 神秘失踪
- 下一篇: MS SQL SERVER导出表结构到E