Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么是错的,如果把P=0加在while函数里面就对了?

Posted by njupt13041032 at 2015-11-05 15:53:42 on Problem 3978
#include <stdio.h>
#include <math.h>
int prime(int x)
{
	int i;
	if(x<=1) return 0;
	else	if(x==2||x==3) return 1;
	else  
	{
		for(i=2;i<=sqrt(x);i++)
		{
			if(x%i==0)
			{
				return 0;
				break;
			}
		}
		return 1;
	}
}
int main()
{
	int a,b,i,p=0;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		
		if((a==-1)&&(b==-1)) return 0;
		for(i=a;i<=b;i++)
		{
			if(prime(i)==1)
				p++;
		}
		printf("%d\n",p);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator