用筛选法求出2-100之间的素数,每行输出5个素数

mazeimz 2009-12-23 09:50:23
老师出的题目,希望各位大虾能帮个忙。尽量简单点,我才学C++几个月
...全文
3066 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunlp007 2011-04-25
  • 打赏
  • 举报
回复

#include <iostream>
#include <cmath>
using namespace std;
bool sushu(int);
int main()
{
int n,flag = 1;
cout<< "2 ";
for (n=3;n<=100;++n)
{
if (n % 2 == 0)
{
continue;
}
if (sushu(n) == false)
{
cout << n << " ";
flag++;
if (flag%5 == 0)
{
cout << endl;
}
}
}
return 0;
}
bool sushu(int n)
{
int i,flag = 0;
for(i=2;i<=n/2;++i)
{
if (n%i==0)
{
flag++;
break;
}
}
if (flag == 0)
return false;
else
return true;
}
JK0803_zhouli 2011-04-24
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "math.h"
int sushu(int n){
int m,flag=1;
m=sqrt(n);
for(int i=2;i<=m;i++)
if(n%i==0)
flag=0;
return flag;
}

main(){
int t;
for(int i=2;i<=100;i++){
t=sushu(i);
if(t)
printf("%d\t\t",i);
}
}
过去的我 2009-12-26
  • 打赏
  • 举报
回复
先要理解筛选法.
艾乐飞 2009-12-26
  • 打赏
  • 举报
回复
好多热心的人啊。
heboucheng 2009-12-25
  • 打赏
  • 举报
回复
#include "stdio.h"
void main()
{
int i,j,n;
for(i=2;i<=100;i++)
{
for(j=2;j<i;j++)
if(i%j==0)
break;
if(i==j&&i!=2)
{
printf("%-3d",i);
n++;
if(n%5==0)
printf("\n");
}
}

}
我也是新学的,希望各位帮忙看看。
EYPHKA 2009-12-25
  • 打赏
  • 举报
回复


范围不用取到数N的一半啊 N的平方就够了·

2~sqrt(n)
MichaelYnag 2009-12-25
  • 打赏
  • 举报
回复
恩不错 !
CCCCCCCCCCCCCCC 2009-12-24
  • 打赏
  • 举报
回复
#include<stdio.h>
int main()
{
int n1,nm,i,j,flag,count=0;

do{
printf("Input START and END=?\n");
scanf("%d%d",&n1,&nm); /*输入求素数的范围*/
}
while(!(n1>0&&n1<nm)); /*输入正确的范围*/
printf("...........PRIME TABLE(%d--%d)............\n",n1,nm);

if(n1==1||n1==2) /*处理素数2*/
{
printf("%4d",2);
n1=3;count++;
}
for(i=n1;i<=nm;i++) /*判定指定范围内的整数是否为素数*/
{
if(!(i%2))continue;
for(flag=1,j=3;flag&&j<i/2;j+=2)
/*判定能否被从3到整数的一半中的某一数所整除*/
if(!(i%j))flag=0; /*若能整除则不是素数*/
if(flag) printf(++count%5?"%4d":"%4d\n",i);
}

return 0;

}


Input START and END=?
2 100
...........PRIME TABLE(2--100)............
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
Press any key to continue
ni009 2009-12-24
  • 打赏
  • 举报
回复
呵呵,有大虾在……
CCCCCCCCCCCCCCC 2009-12-24
  • 打赏
  • 举报
回复
帮你调了一下,结果如下:

please input the start and end number:
2 100
........prime number:2-100...........
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97

Press any key to continue
Ioridy 2009-12-24
  • 打赏
  • 举报
回复
好像不能直接上传截图,没办法上传结果了,不过自己调试了,可以正常运行!
Ioridy 2009-12-24
  • 打赏
  • 举报
回复
一个C++的程序,刚开始学习,试着做了下,还花费了不少时间,有啥问题希望帮提出来,谢谢

#include<iostream>
using namespace std;

void count(int x,int y); //声明count函数
void main()
{
int num1,num2;
cout<<"please input the start and end number:"<<endl;
cin>>num1>>num2;
count(num1,num2); //调用count函数
cout<<endl;
}

void count(int x,int y) //输出素数的函数
{
int flag,num=0;
if (x>0&&x<y) //判断输入的数字合理不
{
cout<<"........prime number:"<<x<<"-"<<y<<"...........\n";

if (x==1||x==2) //如果输入的第一个数小于3,直接输出素数2
{
cout<<"2"<<" ";
num++;
x=3;
}
for (int i=x;i<=y;i++)
{
if (i%2==0) //如果是2的倍数直接跳过
{
continue;
}
flag=3;
while(flag<=i/2 && i%flag!=0) //判断i可以被小于它一半的奇数整除不
{
flag=flag+2;
}
if (flag>i/2) //输出素数
{
num++;
cout<<i<<" ";
if (num%5==0) //每行输出5个
{
cout<<endl;
}
}
}
}
else
{
cout<<"The number is invalid,please INPUT again!";
}
}
CCCCCCCCCCCCCCC 2009-12-23
  • 打赏
  • 举报
回复
一个C程序,供参考

#include<stdio.h>
int main()
{
int n1,nm,i,j,flag,count=0;

do{
printf("Input START and END=?");
scanf("%d%d",&n1,&nm); /*输入求素数的范围*/
}
while(!(n1>0&&n1<nm)); /*输入正确的范围*/
printf("...........PRIME TABLE(%d--%d)............\n",n1,nm);

if(n1==1||n1==2) /*处理素数2*/
{
printf("%4d",2);
n1=3;count++;
}
for(i=n1;i<=nm;i++) /*判定指定范围内的整数是否为素数*/
{
if(!(i%2))continue;
for(flag=1,j=3;flag&&j<i/2;j+=2)
/*判定能否被从3到整数的一半中的某一数所整除*/
if(!(i%j))flag=0; /*若能整除则不是素数*/
if(flag) printf(++count%15?"%4d":"%4d\n",i);
}

return 0;

}


Input START and END=?1 1000
...........PRIME TABLE(1--1000)............
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
53 59 61 67 71 73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359 367 373 379
383 389 397 401 409 419 421 431 433 439 443 449 457 461 463
467 479 487 491 499 503 509 521 523 541 547 557 563 569 571
577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733 739 743 751 757 761
769 773 787 797 809 811 821 823 827 829 839 853 857 859 863
877 881 883 887 907 911 919 929 937 941 947 953 967 971 977
983 991 997Press any key to continue
太乙 2009-12-23
  • 打赏
  • 举报
回复
几个月,应该可以做这样的题目了~~~~~~
mstlq 2009-12-23
  • 打赏
  • 举报
回复
http://dev.csdn.net/article/51/51887.shtm

http://hi.baidu.com/rain_bow_joy/blog/item/66223d44e23f8f3687947387.html

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧