4_21.cpp

来自「C++程序设计技能百练随书配套光盘的源码」· C++ 代码 · 共 27 行

CPP
27
字号
# include <iostream.h>
const int max =1000;
const int width =10;
void main()
{
	bool s[max+1];
	int i,n,col=0;
	for (i=0;i<=max;i++)
		s[i]=0;
	s[0]=1;
	cout<<max<<"以内的素数有:"<<endl;
	for (n=1;n<=max;n++)
		if (s[n-1]==0)
		{
			cout<<n<<"  ";
			if (++col>=width)
			{
				cout<<endl;
				col=0;
			}
			for (i=n;i<=max;i+=n)
				s[i-1]=1;
		}
	if (col!=0)
	cout<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?