#include<stdio.h>int s(int x){int i,y; for (i=0;i<x;i++)if(x%i==0)y=0;else y=1;return y;}main(){int x;scanf("%d",&x);if(s(x)==0)printf("%d is a prime\n",x);else printf("%d is not a prime\n",x);return 0;}这哪里错了啊,求指证!
1 回答
已采纳
望远
TA贡献1017条经验 获得超1032个赞
#include<stdio.h> int s(int x) { int i,y; for (i=2;i<x;i++)//i应该从2开始判断,不能%0 if(x%i==0) { y=0; break;//一旦不是素数,就应该跳出当前循环 } else y=1; return y; } main() { int x; scanf("%d",&x); if(s(x)==1)//这里的条件应该是返回值==1 printf("%d is a prime\n",x); else printf("%d is not a prime\n",x); return 0; }
- 1 回答
- 0 关注
- 2527 浏览
添加回答
举报
0/150
提交
取消