一道大一c语言题,求高手解决,急!谢谢?

2024-11-13 12:55:04
推荐回答(2个)
回答1:

代码资料:

#include "stdio.h"

int prime(int n){//素数判断

int i;

if(n>2 && !(n&1) || n<2)

return 0;

for(i=3;i*i<=n;i+=2)

if(!(n%i))

return 0;

return 1;

}

int main(int argc,char *argv[]){

int n,s;

printf("Enter n(int 1

if(scanf("%d",&n)!=1 || n<2){

printf("Input error, exit...\n");

return 0;

}

for(s=0;n>1;s+=prime(n--));

printf("%d\n",s);

return 0;

}

回答2:

#include

int
func1
(int,int);
int
func2
(int,int);
int
main(void)
{
int
m,n;
printf("Please
input
numbers:");
scanf("%d
%d",&m,&n);
while
(m<=0||n<=0)
{
printf("You
must
input
natural
number,please
try
again:\n");
scanf("%d
%d",&m,&n);
}
printf("%d和%d的最大公约数是:%d\n",m,n,func1(m,n));
printf("%d和%d的最小公倍数是:%d\n",m,n,func2(m,n));
return
0;
}
int
func1
(int
m,int
n)
{
int
temp;
if
(m {
temp=m;
m=n;
n=temp;
}
while(m%n!=0)
{
temp=m%n;
m=n;
n=temp;
}
return
temp;
}
int
func2
(int
m,int
n)
{
return
m*n/func1(m,n);
}