Poj Solution 2739

http://poj.org/problem?id=2739

import java.util.*;
 public class Main{
   public static void main(String args[]){
     Scanner sc=new Scanner(System.in);
      int n=1;
      while(true){
        n=sc.nextInt();
        if(n==0) break;
       System.out.println(test2(n));
      }
      
   }

   /*
    * Sum of Consecutive Prime Numbers 
    */  
  public static int test2(int x){ 
      int count=0; 
      for(int i=2;i+i< x;i++){
           int sum = 0;
           if(!isPrime(i)){
              continue; 
          }
           for(int j=i;j< x;j++){
              // �ж��Ƿ�Ϊ����
              if(!isPrime(j)) 
                 continue;
              sum = sum+j; 
             // �ж��Ƿ��������
              if(sum>=x){
                  if(sum==x){ 
                    count++;
                  } 
                 break;
              }
           } 
      } 
      if(isPrime(x))
           count++;
       return count;
    }

   /*
    * �ж�ij�����Ƿ�Ϊ��������Ƿ���true
    */ 

   public static boolean isPrime(int x){ 
      for(int i=2;i*i<=x;i++){ 
          if(x%i==0) 
             return false; 
      } 
      return true;
    }
}
											
This entry was posted in poj. Bookmark the permalink.