pta 乙级 1013 数素数 java

https://pintia.cn/problem-sets/994805260223102976/problems/994805309963354112

//1013
import java.util.Scanner;
public class Main {
//                输出素数
    public static void print(int p,Long i){
        if(p%10!=0){
            System.out.print(i+" ");
        }
        else if(p%10==0){
            System.out.print(i+"\n");
        }
    }
//            找素数
    public static boolean isPrime(long i){
        if(i==2){
            return true;
        }
        for(int j=2;j*j<=i;j++){
            if(i%j==0){
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int m=in.nextInt();
        int n=in.nextInt();
//
        int t=0;
        int p=0;
        for(long i=2;i>=0;i+=2){
            if(isPrime(i)==true){
                t++;
                if(t>=m&&t<n){
                    p++;
                    print(p,i);
                }
                else if(t==n){
                    System.out.print(i);
                    break;
                }
            }
            if(i<3){
                i--;
            }
        }
    }
}

数素数总是容易出问题,一个是步长为二的前提条件是能够成功输出2而不输出1,其次除数应该<=被除数

发布了20 篇原创文章 · 获赞 0 · 访问量 406

猜你喜欢

转载自blog.csdn.net/weixin_44211856/article/details/104100094