Copyright (c) 2012, 烟台大学计算机学院 All rights reserved. 作 者: 刘元龙 完成日期:2012 年11 月 11日 版 本 号:v1.0 输入描述:输出1000以内所有素数 07.问题描述:利用穷举法输出所有的方案 08.程序输出:穷举法 #include <iostream> #include <cmath> using namespace std; int main () { int x,y,z; bool prime; for(x=2;x<1000;x++) { prime=true; y=int(sqrt(x)); for(z=2;z<=y;z++) { if(x%z==0) { prime=false; break; } } if(prime) cout<<x<<" "; else cout<<" "; } cout<<endl; return 0; }
运行结果:
心得体会:做他花了好长时间,但愿有所收获!