zoukankan      html  css  js  c++  java
  • 九度oj1163题

    题目描述:

    输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数,如果没有则输出-1。

    输入:

    输入有多组数据。
    每组一行,输入n。

    输出:

    输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数(素数之间用空格隔开,最后一个素数后面没有空格),如果没有则输出-1。

    样例输入:
    100
    样例输出:
    11 31 41 61 71

    2017-03-0913:02:22
    一预先存储法
     1 package a;
     2 import java.util.*;
     3 public class Main{     
     4     public static  void main(String[] args)  {   
     5         Scanner cin = new Scanner(System.in);      
     6         boolean arr[]=new boolean[10001];
     7         int []arr1=new int [10000];
     8          for (int i=1;i<=10000;i++){
     9              arr[i]=false;
    10          }
    11          int size=0;
    12          for(int i=2;i<=10000;i++){
    13              if (arr[i]==true)//表示非素数。
    14              {
    15                  continue;
    16              }else {
    17                  arr1[size++]=i;//经过1-10的筛选1-10000的非素数基本上都被标记上了。
    18                  for(int j=i*i;j<=10000;j+=i){
    19                      arr[j]=true;
    20                  }
    21              }
    22          }        
    23         while(cin.hasNext()){
    24             int  n=cin.nextInt();        
    25             boolean lab=false;
    26             boolean lab1=false;
    27             for(int i=0;i<size;i++){
    28                 if(arr1[i]<n&&arr1[i]%10==1){
    29                     lab=true;
    30                     if(lab1==false){
    31                         System.out.print(arr1[i]);
    32                         lab1=true;
    33                             }else{
    34                                 System.out.print(" "+arr1[i]);
    35                             }
    36                 }
    37           }if(lab)
    38               System.out.println();              
    39            else System.out.println("-1");
    40         
    41         }
    42         cin.close();  
    43     }
    44    
    45    
    46 } 

    二笨方法
     1 import java.util.*;
     2 public class Main{   
     3     public static  void main(String[] args)  {   
     4         Scanner cin = new Scanner(System.in);
     5          //System.out.println("**********");        
     6         while(cin.hasNext()){
     7             int  n=cin.nextInt();        
     8             boolean lab=false;
     9               if(n<=10){
    10                   System.out.println("-1");
    11               }
    12             else{
    13                 int count=0;
    14                 boolean lab1=false;
    15                 for(int j=11;j<n;j++){
    16                      
    17                 int b=(int) (Math.sqrt(j)+1);
    18                 for(int i=2;i<b;i++){
    19                     if(j%i!=0){
    20                         lab=true;
    21                     }else{
    22                         lab=false;
    23                         break;                              
    24                     }
    25                  
    26             }
    27                 if(lab){
    28                     if(j%10==1){
    29                         count++;
    30                         if(lab1==false){
    31                     System.out.print(j);
    32                     lab1=true;
    33                         }else{
    34                             System.out.print(" "+j);
    35                         }
    36                              
    37                     }
    38                     else{
    39                         continue;
    40                     }
    41                 }else
    42                 {
    43                     continue;
    44                 }
    45             }
    46                  
    47                  if(count==0)
    48                   System.out.println("-1");
    49                  else System.out.println();
    50                  
    51             }
    52               
    53         }
    54         cin.close();  
    55     }
    56     
    57     
    58 }


  • 相关阅读:
    jquery、js调用iframe父窗口与子窗口元素的方法整理
    js中的各种宽高以及位置总结
    javascript call与apply关键字的作用
    javascript之window对象
    CSS3 3D立方体效果
    CSS3 3D transform变换
    JS查找字符串中出现次数最多的字符
    写一个函数将传入的字符串转换成驼峰表示法
    数组去重方法
    内核升级得一个模范
  • 原文地址:https://www.cnblogs.com/wwwhza/p/6525158.html
Copyright © 2011-2022 走看看