zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯VIP 算法提高 特殊的质数肋骨

    算法提高 特殊的质数肋骨
    时间限制:1.0s 内存限制:256.0MB
    问题描述
      农民约翰母牛总是产生最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数。

    例如有四根肋骨的数字分别是:7 3 3 1,那么全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。7331 被叫做长度 4 的特殊质数。

    写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数。数字1不被看作一个质数。
    输入格式
      单独的一行包含N。
    输出格式
      按顺序输出长度为 N 的特殊质数,每行一个。
    样例输入
    4
    样例输出
    2333
    2339
    2393
    2399
    2939
    3119
    3137
    3733
    3739
    3793
    3797
    5939
    7193
    7331
    7333
    7393

    import java.util.Scanner;
    
    
    public class 特殊的质数肋骨 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		n--;
    		/**首位数字为素数的只有2,3,5,7,*/
    		f(2, n);//寻找首位数字为2的满足条件度数
    		f(3, n);//寻找首位数字为3的满足条件度数
    		f(5, n);//寻找首位数字为5的满足条件度数
    		f(7, n);//寻找首位数字为7的满足条件度数
    	}
     
    	private static void f(long i, int n) {
    		if (n == 0) {
    			if (su(i))
    				System.out.println(i);
    		}
    		if (su(i)) {
    			/**个位数字是偶数不可能为素数*/
    			f(i * 10 + 1, n - 1);
    			f(i * 10 + 3, n - 1);
    			f(i * 10 + 5, n - 1);
    			f(i * 10 + 7, n - 1);
    			f(i * 10 + 9, n - 1);
    		}
    	}
    	private static boolean su(long n) {
    		for (int i = 2; i * i <= n; i++) {
    			if (n % i == 0)
    				return false;
    		}
    		return true;
    	}
    
    }
    
    
  • 相关阅读:
    LeetCode 1110. Delete Nodes And Return Forest
    LeetCode 473. Matchsticks to Square
    LeetCode 886. Possible Bipartition
    LeetCode 737. Sentence Similarity II
    LeetCode 734. Sentence Similarity
    LeetCode 491. Increasing Subsequences
    LeetCode 1020. Number of Enclaves
    LeetCode 531. Lonely Pixel I
    LeetCode 1091. Shortest Path in Binary Matrix
    LeetCode 590. N-ary Tree Postorder Traversal
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948361.html
Copyright © 2011-2022 走看看