----------------解决方案--------------------------------------------------------
这种题好像书上都有答案的
----------------解决方案--------------------------------------------------------
呵呵..我不会做.可不可以帮我编一下
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <math.h>
int judge(int);
void main(void)
{
int num;
clrscr();
printf("Please input num:\n");
scanf("%d",&num);
if(0==judge(num))
printf("%d is not su shu\n",num);
else
printf("%d is su shu\n",num);
getch();
}
int judge(int num)
{
int i;
for(i=2;i<=sqrt(num);i++)
if(num%2==0)
return 0;
return 1;
}
----------------解决方案--------------------------------------------------------
#include "stdio.h"
void Prime(int);
int main(void){
unsigned int n;
scanf("%u",&n);
Prime(n);
return 0;
}
void Prime(int n){
int i,k=0;
if(n!=2&&n%2==0)
printf("the number is not prime.");
else{
do{
i=3;
if(n==i||n%i!=0)
k=1;
i+=2;
}while(i*i<=n);
if(k==1)
printf("the number is prime.");
else
printf("the number is not prime.");
}
return ;
}
----------------解决方案--------------------------------------------------------
[QUOTE]#include <stdio.h>
#include <conio.h>
#include <math.h>
int judge(int);
void main(void)
{
int num;
clrscr();
printf("Please input num:\n");
scanf("%d",&num);
if(0==judge(num))
printf("%d is not su shu\n",num);
else
printf("%d is su shu\n",num);
getch();
}
int judge(int num)
{
int i;
for(i=2;i<=sqrt(num);i++)
if(num%2==0)
return 0;
return 1;
}[/QUOTE]
个人感觉效率不是很好.
----------------解决方案--------------------------------------------------------
#include "stdio.h"
void Prime(int);
int main(void){
unsigned int n;
scanf("%u",&n);
Prime(n);
return 0;
}
void Prime(int n){
int i,k=0;
if(n!=2&&n%2==0)
printf("the number is not prime.");
else{
do{
i=3;
if(n==i||n%i!=0){
k=1;
i+=2;
}
else{
k=0;
break; //刚才漏掉了
}
}while(i*i<=n);
if(k==1)
printf("the number is prime.");
else
printf("the number is not prime.");
}
return ;
}
----------------解决方案--------------------------------------------------------
#include <conio.h>
#include <math.h>
int judge(int);
void main(void)
{
int num;
clrscr();
printf("Please input num:\n");
scanf("%d",&num);
if(0==judge(num))
printf("%d is not su shu\n",num);
else
printf("%d is su shu\n",num);
getch();
}
int judge(int num)
{
int i;
for(i=2;i<=sqrt(num);i++)
if(num%2==0)
return 0;
return 1;
}
个人感觉效率不是很好.
我想请您试一下,有没有得到正确答案?
----------------解决方案--------------------------------------------------------
...
那楼上的人为num%2==0处理好捏?
我也想知道捏。。。
----------------解决方案--------------------------------------------------------
哦 错了,
应该是
if(num%i==0)
----------------解决方案--------------------------------------------------------