鹿晗关晓彤表情包:VC++编程:这个该怎么编啊?

来源:百度文库 编辑:神马品牌网 时间:2024/05/03 04:54:09
编写一个程序,输入所有小于等于n(n为一个大于2的正整数)的素数。要求每行输入10素数。

这是我以前编的:
算法一,筛选法
#include <iostream.h>

void main()
{
int i,j,k=0;
int max;
cout<<"input the max:";
cin>>max;
cout<<endl;
int *Sky=new int[max+1];
for(i=1;i<=max;i++)
*(Sky+i)=0;

for(i=2;i<=max;i++)
if(*(Sky+i)==0)
for(j=i+i;j<=max;j+=i)
*(Sky+j)=1;

for(i=2;i<=max;i++)
if(!*(Sky+i))
{ cout<<i<<"\t"; if(++k%10==0) cout<<endl;}
cout<<endl;
cin>>i;

delete []Sky;
}

算法二,循环判断法:
#include <iostream.h>

void main()
{
int i,j,k=0;
bool condition=false;
int max;
cout<<"input the max:";
cin>>max;
cout<<endl;
for(i=2;i<=max;i++)
{
for(j=2;j<i;j++)
if(i%j==0)
{condition=true;break;}

if(condition){condition=false;continue;}
cout<<i<<"\t";
if(++k%10==0)
cout<<endl;
}
cout<<endl;
}
programmed by sky_dragon_bigc@yahoo.com.cn