zoukankan      html  css  js  c++  java
  • 华为机试测试-找偶数的相距最近的素数对

     1 import java.util.Scanner;
     2 public class Main{
     3       public static void main(String[] args){
     4           Scanner scanner=new Scanner(System.in);
     5           int n=scanner.nextInt();
     6           for(int i=n/2;i>=2;i--)
     7           {
     8               if(isSushu(i) && isSushu(n-i))
     9               {
    10                   System.out.println(i);
    11                   System.out.println(n-i);
    12                   break;
    13               }
    14           }
    15           scanner.close();
    16       } 
    17       public static boolean isSushu(int x)
    18       {
    19           if(x==1) return false;
    20           if(x==2 || x==3 || x==5 || x==7) return true;
    21           if(x%2==0 || x%3==0 || x%5==0 || x%7==0) return false;
    22           for(int i=3;i<x/2;i++)
    23           {
    24               if(x%i==0)
    25               {
    26                   return false;
    27               }
    28           }
    29           return true;
    30       }
    31 }
  • 相关阅读:
    Manacher算法
    [SCOI2010]生成字符串
    [HNOI2008]GT考试
    矩阵乘法
    关于主席树
    Spring 书籍
    Spark书籍
    TimeMeasure
    Dynamic login
    Image quality evaluation
  • 原文地址:https://www.cnblogs.com/maydow/p/4781527.html
Copyright © 2011-2022 走看看