| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 702 人关注过本帖
标题:function质数问题
只看楼主 加入收藏
alicefioan37
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-9-29
收藏
 问题点数:0 回复次数:6 
function质数问题

int isPrime(int n)那部分好像不行,输入3,30,只打印3, 后面就通不过,大侠们帮忙看看啊,


#include<stdio.h>
#include <conio.h>
#include <math.h>

int isPrime(int);
int findPrime (int, int ) ;
int findSquare(int,int);

int sum;

int main (void)
{
int x,y,sumPrime;

printf("Enter two numbers\n");
scanf("%d %d",&x,&y);

while(x <= y)
{
printf("Prime: ");
sumPrime=findPrime( x, y);

printf("\nSquare: ");
findSquare(x,y);

printf("\nsum of all primes and squares:%d",sum);

printf("\nEnter two numbers");
scanf("%d %d",&x,&y);
}
return 0;
}

int findPrime (int a, int b )

{

int sumPrime=0,c=0;

while(a<=b)

{

if(isPrime(a))

{

printf("%d ",a);
c++;
sumPrime+=a;
a++;

}

}

printf("\nPrime count = %d\n",c);

return 0 ;

}

int isPrime(int n)

{

int i;

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

if(n%i==0)

return 0;

return 1;

}

int findSquare ( int a,int b)

{

int i,t,c=0,sumSquare = 0;

for(i=2; i*i<=b;i++)

{

printf("%d ", i*i);

c++;

sumSquare+=i*i;

}

printf("\nsumsquare is %d",sumSquare);

printf("\nSquare count = %d\n",c);

return sumSquare;

}

搜索更多相关主题的帖子: 质数 function 
2007-11-06 00:46
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 


int findPrime (int a, int b ) /* 这个函数死循环 */
{
int sumPrime = 0, c = 0;

while(a <= b)
{
if(isPrime(a))
{
printf("%d ",a);
c++;
sumPrime += a;
a++; /* a++ 因该放在 if 外面 */
}
/* a++; // 这里 */
}

printf("\nPrime count = %d\n",c);

return 0;
}

/* 就这个问题,其它没问题 (,那个函数看错了) */

[此贴子已经被作者于2007-11-6 1:56:52编辑过]


—>〉Sun〈<—
2007-11-06 01:17
alicefioan37
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-9-29
收藏
得分:0 
回复:(cosdos)int isPrime(int n) /* 这个函...
谢谢,明白了
2007-11-06 01:48
BTMouse
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-8-23
收藏
得分:0 


isPrime没问题,根据你这里写的这个函数是判断素数的,正确。

错的是findPrime函数,改正如下:

int findPrime (int a, int b )

{

int sumPrime=0,c=0;

printf("enter: ");

printf("%d %d\n",a,b);

while(a<=b)

{

if(isPrime(a))


{

printf("%d ",a);
c++;
sumPrime+=a;
a++;

}

else
a++; // 没有这个当a=4的时候就进入死循环了,因为4<30而你又不给a++,所以打出3后就没反应了


}

printf("\nPrime count = %d\n",c);

return 0 ;

}


2007-11-06 01:51
BTMouse
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-8-23
收藏
得分:0 

寒,早了两分钟被人抢了

2007-11-06 01:52
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
回复:(BTMouse)寒,早了两分钟被人抢了
呵呵,
我一个函数看错了

—>〉Sun〈<—
2007-11-06 01:57
alicefioan37
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-9-29
收藏
得分:0 
谢谢各位
2007-11-06 02:07
快速回复:function质数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012940 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved