[PAT]1015. Reversible Primes (20)@Java
来源:互联网 发布:win10软件模糊 编辑:程序博客网 时间:2024/06/12 06:22
1015. Reversible Primes (20)
A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.
Now given any two positive integers N (< 105) and D (1 < D <= 10), you are supposed to tell if N is a reversible prime with radix D.
Input Specification:
The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.
Output Specification:
For each test case, print in one line "Yes" if N is a reversible prime with radix D, or "No" if not.
Sample Input:73 1023 223 10-2Sample Output:
YesYesNo
分析:
判断是否是质数,以及加上数制转换。
这里的是指原数字与它在数制下的倒叙,如23 2,是10111(2)的倒叙11101的真实数字。判断原数与倒叙数字是否都是质数判别。
案例分析:
73 10,73是质数,10进制下的倒叙数是37,也是质数,Yes
23 2, 23是质数,2进制下表示为10111,倒叙数是11101,转换为10进制为29,也是质数,Yes
23 10,23是质数,10进制下的倒叙数是32,不是质数,No
package go.jacob.day919;import java.util.ArrayList;import java.util.Scanner;/** * [PAT]Reversible Primes (20) * @author Jacob * 两个知识点:1.将十进制转化为任意进制; * 2.判断是否为素数 */public class Demo1 {public static void main(String[] args) {Scanner sc=new Scanner(System.in);ArrayList<Integer[]> list=new ArrayList<Integer[]>();while(sc.hasNext()){int n=sc.nextInt();if(n<0)break;int d=sc.nextInt();list.add(toDecimal(n,d));}for(Integer[] nums:list){if(isPrime(nums[0])&&isPrime(nums[1]))System.out.println("Yes");elseSystem.out.println("No");}sc.close();}//转化为任意进制,返回原数和“逆”数//(逆数需要把原数转化成d进行,逆序,再转成十进制)private static Integer[] toDecimal(int n, int d) {Integer[] res=new Integer[2];res[0]=n;if(d==10){StringBuilder sb=new StringBuilder(n+"");res[1]=Integer.parseInt(sb.reverse().toString());return res;}String digits = "0123456789"; StringBuilder sb=new StringBuilder();while(n!=0){sb.append(digits.charAt(n%d));n=n/d;}int sum=0;for(int i=0;i<sb.length();i++){sum=sum*d+(sb.charAt(i)-'0');}res[1]=sum;return res;}//判断是否是质数private static boolean isPrime(int num) {if(num==1)return false;if(num==2)return true;int max=(int) Math.ceil(Math.sqrt(num));for(int i=2;i<=max;i++){if(num%i==0)return false;}return true;}}
- [PAT]1015. Reversible Primes (20)@Java
- 1015. Reversible Primes (20)-PAT
- 【PAT】1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- pat 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT 1015. Reversible Primes (20)
- PAT--1015. Reversible Primes
- PAT 1015. Reversible Primes
- 【PAT】1015. Reversible Primes
- 【PAT Advanced Level】1015. Reversible Primes (20)
- PAT A 1015. Reversible Primes (20)
- 四、swift3.0函数的使用
- Android中判断网络是否连接(可用),不可用跳转设置网络连接
- Matlab学习笔记(5)——classify函数实现线性判别分析
- 计算一组向量相似度
- 接口形式实例化-匿名内部类
- [PAT]1015. Reversible Primes (20)@Java
- XML创建Fragment
- Java生成和操作Excel文件
- 函数和链接性
- python网络爬虫-使用API之API通用规则
- 【17】关于HTTP协议,一篇就够了
- 劳动仲裁
- ORACLE安装+PLSQL12.0.5+汉化包+注册码
- Python数字与字符之间的转换