帮忙看看这个程序.是否可以简化

saishow 2010-07-31 12:58:46
//编写一个程序,接受一个整数输入,然后显示所有小于或者等于该数的素数.
	int n,i = 0;
scanf("%d",&n);
while (n > 1){
if (n == 2|| n % 2)
if (n == 3 || n % 3)
if (n == 5 || n % 5)
if (n == 6 || n % 6)
if (n == 7 || n % 7)
if (n == 8 || n % 8)
if (n == 9|| n % 9){
printf("%d\t",n);
i++;
}
n--;
}
printf("有%d素数\n",i);
...全文
148 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
eye_119_eye 2010-08-01
  • 打赏
  • 举报
回复
?都这么写呀!
wxwlll 2010-08-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 saishow 的回复:]
引用 2 楼 wxwlll 的回复:
这是我写的:

C/C++ code
#include<stdio.h>
#include<math.h>
main()
{
int a,b,i,count=0;
scanf("%d",&amp;a);
for(b=2;b<=a;b++) //因为1不是素数
{
for(i=2;i<=(int)sqrt(b);i++)
……
……
[/Quote]

那就加一句判断,先判断这个数是否大于3,如果是,单独把2和3输出。不过这个数是2,就直接输出2,如果是3,就输出2和3!
kxw0620 2010-08-01
  • 打赏
  • 举报
回复
这道题就是判断一个数是否素数....
下面代码应该可以满足....
#include <stdio.h>
#include <math.h>
void main()
{
int num;
int i,j,k;

scanf("%d",&num);
for(i = 2; i <= num; i++)
{
k = (int)sqrt(i);
for(j = 2; j <= k; j++)
{
if(i%j == 0)
{
break;
}
}
if(j > k)
{
printf("%d\t",i);
}
}

return ;
}
qgqch2008 2010-08-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 saishow 的回复:]

引用 6 楼 qgqch2008 的回复:
C/C++ code
#include<stdio.h>
int main()
{
int a,i,j,m;
scanf("%d",&amp;a);
for(i=2;i<=a;i++)
{
m=1;
for(j=2;j*j<i;j++)
if(i%j==0)m=0;
……


兄弟错了啊!
[/Quote]
#include<stdio.h>
int main()
{
int a,i,j,m;
scanf("%d",&a);
for(i=2;i<=a;i++)
{
m=1;
for(j=2;j*j<=i;j++)//少了一个等号,这下对了
if(i%j==0)m=0;
if(m)printf("%4d",i);
}
return 0;
}
qgqch2008 2010-08-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 saishow 的回复:]

引用 6 楼 qgqch2008 的回复:
C/C++ code
#include<stdio.h>
int main()
{
int a,i,j,m;
scanf("%d",&amp;a);
for(i=2;i<=a;i++)
{
m=1;
for(j=2;j*j<i;j++)
if(i%j==0)m=0;
……


兄弟错了啊!
[/Quote]哪儿错了啊?指教一下啊
saishow 2010-08-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qgqch2008 的回复:]
C/C++ code
#include<stdio.h>
int main()
{
int a,i,j,m;
scanf("%d",&a);
for(i=2;i<=a;i++)
{
m=1;
for(j=2;j*j<i;j++)
if(i%j==0)m=0;
……
[/Quote]

兄弟错了啊!
elegant87 2010-07-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qgqch2008 的回复:]

C/C++ code
#include<stdio.h>
int main()
{
int a,i,j,m;
scanf("%d",&a);
for(i=2;i<=a;i++)
{
m=1;
for(j=2;j*j<i;j++)
if(i%j==0)m=0;
if(m)pri……
[/Quote]
这个简单
qgqch2008 2010-07-31
  • 打赏
  • 举报
回复
#include<stdio.h>
int main()
{
int a,i,j,m;
scanf("%d",&a);
for(i=2;i<=a;i++)
{
m=1;
for(j=2;j*j<i;j++)
if(i%j==0)m=0;
if(m)printf("%4d",i);
}
return 0;
}
我写的,楼主参考
canshui 2010-07-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 saishow 的回复:]
引用 1 楼 canshui 的回复:
看是你要的效果不!

C/C++ code
#include <stdio.h>
#include <math.h>
int test(int n) ;
void main()
{
int n, i=0, count=0 ;
scanf("%d", &amp;n) ;
while(n>=1) {

if(test(n)) {……
[/Quote]
哦,是!!多谢提醒……
saishow 2010-07-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxwlll 的回复:]
这是我写的:

C/C++ code
#include<stdio.h>
#include<math.h>
main()
{
int a,b,i,count=0;
scanf("%d",&a);
for(b=2;b<=a;b++) //因为1不是素数
{
for(i=2;i<=(int)sqrt(b);i++)
……
[/Quote]

100
5 7 11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71 73 79
83 89 97

还有 2 3 不对!
saishow 2010-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 canshui 的回复:]
看是你要的效果不!

C/C++ code
#include <stdio.h>
#include <math.h>
int test(int n) ;
void main()
{
int n, i=0, count=0 ;
scanf("%d", &n) ;
while(n>=1) {

if(test(n)) {
……
[/Quote]
while(n>=1)要去掉=
wxwlll 2010-07-31
  • 打赏
  • 举报
回复
这是我写的:
#include<stdio.h>
#include<math.h>
main()
{
int a,b,i,count=0;
scanf("%d",&a);
for(b=2;b<=a;b++) //因为1不是素数
{
for(i=2;i<=(int)sqrt(b);i++)
{
if(i==(int)sqrt(b)&&b%i!=0)
{
printf("%d ",b);
count++;
break;
}
if(b%i==0)
{
break;
}
}
if(count==10)
{
printf("\n");
count=0;
}
}
printf("\n");
}
canshui 2010-07-31
  • 打赏
  • 举报
回复
看是你要的效果不!
#include <stdio.h>
#include <math.h>
int test(int n) ;
void main()
{
int n, i=0, count=0 ;
scanf("%d", &n) ;
while(n>=1) {

if(test(n)) {
printf("有素数%d\n", n) ;
count++ ;
}
n--;
}
printf("有%d个素数\n", count) ;
}
int test(int n)
{
int i ;
for(i=2 ; i<=sqrt(n) ; i++)
if(!(n%i))
return 0;
return 1 ;
}

睡觉去了…………

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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