c++ 求输入m和n,输出m到n间的所有素数。

一楼的太高深了~请用常见点的回答 谢谢

二楼的不对~

#include<iostream>
using namespace std;

int main(){
int i,j,m,n;
cin>>m>>n; //假定m<=n 且m>1 因为1既不是素数也不是合数
for(i=m;i<=n;i++){
for(j=2;j<=i/2;j++)
if(i%j == 0) break;
if(j>i/2) cout<<i<<endl;
}
//return 1;
system("pause");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-09-28
bool isPrimeNumber(int n){略;}
int getPrimeNumbers(int m,int n){
int total=0;
int i=0;
int temp;
if(m>n){temp=m;m=n;n=temp;}
for(i=m;i<=n;i++){
if(isPrimeNumber(i))total++;
}
teturn total;
}
第2个回答  2008-09-28
#include <iostream>
using std::cin;
using std::cout;

main()
{
int i,j,a=1,s=0,qr,m,n,t;
cout<<"请输入M和N"<<endl;
cin>>m>>n;
if(n<m)
{
t=m;
m=n;
n=t;
}
for(i=m;i<n;i++)
{
qr= (int)sqrt(i);
for(j=2;j<qr;j++)
if(!(i%j)) {a=0;break;}//如果i是合数,a=0,跳出循环
else a=1;
if(a) s+=i;//如果a是质数,s累加
}
cout << s << endl;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网