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

筛选加暴力 16MS

Posted by zjp at 2009-08-17 13:47:18 on Problem 2739
#include <stdio.h>
     int main ()
     {
     	  int a[10001];
     	  int temp;int k=0;int i;int sum;int n;
		  int b[10000];
     	  int flag;int num;
     	  for (i=2;i<=10000;i++)
     	    a[i]=1;
     	  for (i=2;i<=5000;i++)
     	      {  
     	             if (a[i]==1)
					 {  temp=2*i;
     	        	 while (temp<=10000)
     	               { 
     	                a[temp]=0;
						temp+=i;
					 }
					 }
     	        }   
     	  for  (i=2;i<10001;i++)
     	      if (a[i]==1)
                 b[++k]=i; 
			  
			  
     	   while (EOF!= scanf("%d",&n))
     	   {
     	   	     if (n==0)
     	   	     	 break;
     	   	     flag=1;num=0;
     	   	     while (b[flag]<=n)
     	   	     {  sum=0;
     	   	     	for (i=flag;;i++)
     	   	     	  {    sum=sum+b[i];
     	   	     	      if (sum==n)
     	   	     	      {	num++;break;}
     	   	     	      	if (sum>n)
     	   	     	      		break;
     	   	     	  }	 
     	   	     	   flag++;     
     	         	}  
     	   	
     	   	
     	   	printf("%d\n",num);
     	   	
     	   	}
     	
     	
     	
     	
     	}
     
     
     
     
     

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