首页 > 其他分享 >PAT 甲级【1015 Reversible Primes】

PAT 甲级【1015 Reversible Primes】

时间:2023-10-28 22:24:05浏览次数:26  
标签:StreamTokenizer PAT java int System st io 1015 Primes

  1. 考察素数判断
  2. 考察进制转换
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {
    @SuppressWarnings("uncheck")
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in));
        int n, d;
        while (true) {
            st.nextToken();
            n = (int) st.nval;
            if (n < 0) {
                break;
            }
            st.nextToken();
            d = (int) st.nval;
            String str = Integer.toString(n, d);
            String reversestr = new StringBuilder(str).reverse().toString();

            int p2 = Integer.parseInt(reversestr, d);

            if( isPrime(n) && isPrime(p2)){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }

    public static boolean isPrime(int n) {
        if (n == 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}

 

标签:StreamTokenizer,PAT,java,int,System,st,io,1015,Primes
From: https://www.cnblogs.com/fishcanfly/p/17794771.html

相关文章

  • PAT甲级【1014 Waiting in Line】
    考察双向链表importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;importjava.util.LinkedList;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOExcepti......
  • PAT 甲级【1013 Battle Over Cities】
    本题就是dfs.连通图个数-2;但是java慢,最后一个case超时importjava.io.*;importjava.util.HashSet;importjava.util.Set;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOException{StreamToken......
  • PAT_B1008 数组元素循环右移问题
    一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0​A1​⋯AN−1​)变换为(AN−M​⋯AN−1​A0​A1​⋯AN−M−1​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?输入格式:每......
  • 无涯教程-Clojure - re-pattern函数
    re-pattern返回java.util.regex.Pattern的实例。然后将其用于其他模式匹配方法。re-pattern-语法(repatternpat)参数   - "pat"是需要形成的pattern。返回值 - 类型为java.util.regex.Pattern的模式对象。re-pattern-示例(nsclojure.examples.example......
  • [Spring框架学习]SSM 整合,使用maven构建项目的时候,启动项目报错class path resource
    错误:classpathresource[config/spring/springmvc.xml]cannotbeopenedbecauseitdoesnotexist错误原因:找不到我的springmvc.xml,在下面web.xml中是我引用路径,网上找到问题classpath指向路径不是resource路径,所以一直找不到我的xml文件,classpath:到你的class路径......
  • PAT_A1049 Counting Ones【困难】
    Thetaskissimple:givenanypositiveinteger N,youaresupposedtocountthetotalnumberof1'sinthedecimalformoftheintegersfrom1to N.Forexample,given N being12,therearefive1'sin1,10,11,and12.InputSpecification:Eac......
  • PAT_A1104 Sum of Number Segments
    Givenasequenceofpositivenumbers,asegmentisdefinedtobeaconsecutivesubsequence.Forexample,giventhesequence{0.1,0.2,0.3,0.4},wehave10segments:(0.1)(0.1,0.2)(0.1,0.2,0.3)(0.1,0.2,0.3,0.4)(0.2)(0.2,0.3)(0.2,0.3,0.4)......
  • PAT 甲级【1012 The Best Rank】
    本题用java极容易超时,提交了好几次才成功另外9088777750,名次应该是12335,不是12334 importjava.io.*;publicclassMain{@SuppressWarnings("unchecked")publicstaticvoidmain(String[]args)throwsIOException{StreamTokenizer......
  • PAT_B1003 我要通过!
    “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送——只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。得到“答案正确”的条件是:字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;任意形如 xPA......
  • gerrit 将他人改动直接 打patch到自己代码上
    原文:https://blog.csdn.net/qq_21438461/article/details/131362485 在Linux中,patch命令用于将补丁文件应用到源代码文件中,从而实现对源代码的修改。patch命令的详细描述如下:patch命令用于将补丁文件应用到源代码文件中,以实现对源代码的修改。补丁文件通常是由开发者或者社区......