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

筛选法求素数,然后水过。。。

Posted by lijiaming12340 at 2013-10-27 17:58:13 on Problem 2739
#include<stdio.h>
int main()
{
 int a[12001],b[1000],i,j,k,n,x,head,tail,sum,ans;
 x=0;
 memset(a,0,sizeof(a));
 for (i=2; i<=12000; i++) 
     if (!a[i]) 
	 {
	  x++; 
	  b[x]=i;
	  for (j=i; j<=12000; j+=i)	a[j]=1;
 	 }  
 scanf("%d",&n);
 while (n!=0)
 {
  ans=0;	
  sum=0;
  head=1;
  tail=1;
  do
  {
   k=0;
   while (sum+b[tail]<=n)
   {
   	sum+=b[tail];
   	tail++;
   	k=1;
   }
   if (sum==n && k) ans++;
   sum+=b[tail];
   tail++;
   k=0;
   while (sum>n)
   {
   	sum-=b[head];
   	head++;
   	k=1;
   }
   if (sum==n && k) ans++;
  }while (head<tail);
  printf("%d\n",ans);
  scanf("%d",&n);
 }
 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