问题描述:
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
解决问题:
这题和Problem24差不多。都是全排列的问题。
不过这里我们得找到所有的排列数字。然后判断是否是素数。
P24是让我们找到第n个排列,我们可以从1一直找到最后一个~
不过使用24的方法肯定是不行的。
最好的方法就是从小到大挨个找。
这里使用递归的方法。
假如找到4位数的全排列,n4n3n2n1
先令初始的可以选择的数为{1,2,3,4}
令n1=1,则n2只能在{1,2,3,4}-{1} = {2,3,4}的集合里找
再n2=2,则n3只能在{2,3,4}-{2}={3,4}的几个里找
算法:
public static void find(int level, boolean[] elements, long result) {
if (level == 1) {
if(IsPrime(result)){
if(max<result)
max = result;
}
// System.out.println(result);
return;
}
for (int i = 1; i < elements.length; i++) {
if (elements[i]) {
elements[i] = false;
long tmp = result;
result = result * 10 + i;
find(level - 1, elements, result);
elements[i] = true;
result = tmp;
}
// System.out.println("Level:"+level+",i:"+i+",Result:"+result);
}
}
分享到:
相关推荐
0-1-knapsack-problem-master (41).zip
据说着名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1...
Problem41 of LeetCode 题目: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数。 这道题体现了对数组信息维度的利用。利用好了可以在时间和空间复杂度上都有很大帮助 可以通过数组的索引和值达到类似于...
离线安装包,亲测可用
打地鼠
联想商用机开天M6600 BIOS 20KT41AUS,官网上未提供。这是官方提供的,刷BIOS程序在里面,用纯DOS启动才能运行,不能加EMM386以上的内存管理程序等,否则不能运行。启动直接运行MB.bat,自动备份和刷新。 刷机风险,...
官方离线安装包,亲测可用
problem. The statement of the problem specifies in general terms the desired input/output relationship. The algorithm describes a specific computational procedure for achieving that input/output ...
约瑟夫问题(Josephus Problem)据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人到,于是决定了一个自杀...
41 1.414 EFFECTS OF INYOLVING CONDUCT PROBLEM ADOLESCENTS IN GOAL SETTING: AN EXPLORATORY INVESTIGATION CHARLES A. M A H E R Rutgers University Effects of involving conduct problem ...
41, , , 44, 45, , , , , , 51, 52, , , , , 57, , , , , , , , 65, , , 68, , , , , , , , 76, , , , , 81, , , 84, 85, , 87, , , , 91, , 93, , , , 97, , 99, , , , , , 105, 106, , , , , , , , , 115, 116, ...
2.11 A General Mathematical Model for Optimum Design 41 2.11.1 Standard Design Optimization Model 42 2.11.2 Maximization Problem Treatment 43 2.11.3 Treatment of “Greater Than Type” Constraints 43 ...
41 3.6.6 Preloaded turnstile . . . . . . . . . . . . . . . . . . . . . . 43 3.6.7 Barrier objects . . . . . . . . . . . . . . . . . . . . . . . . 44 3.7 Queue . . . . . . . . . . . . . . . . . . . . ...
5 LOGSPACE, Random Walks on Graphs, and Universal Traversal Sequences 41 6 Exponential Lower Bounds for the Length of Resolution Proofs 49 7 Spectral Problems and Descriptive Complexity Theory 61 8 ...
10.6 The Best Flow Problem—Not a Classical Graph Problem 165 10.6.1 Income Model for Customer Charges and Notations 166 10.6.2 Noncontinuous Price Function Paid to the Interworking Network for an ...
循环码matlab中编程代码社交蜘蛛算法 ...solution) { // Your objective function implementation. return 0.0; } }; SSA ssa(new MyProblem(30), 30); ssa.run(10000, 1.0, 0.7, 0.1); Python def problem(x
CHAPTER 41 - Approximation Schemes for Fractional Multicommodity Flow Problems CHAPTER 42 - Approximation Algorithms for Connectivity Problems CHAPTER 43 - Rectilinear Steiner Minimum Trees CHAPTER 44...
VCS) is a very wise thing to use. It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last ...
Abstract— With the increasing number of computers being connected to the Internet, security of an information system has never been more urgent. Because no system can be absolutely ...IDS).
3.1.1 Strings Are Sequences of Unicode Characters 41 3.1.2 String Manipulation 42 3.1.3 Methods Associated with Strings 42 3.2 LISTS 44 3.2.1 Accessing List Elements 45 3.2.2 List with Multiple ...