- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在应对一项编程挑战,其中一个问题涉及包含/排除原则问题,我认为我已经解决了这个问题,但在一些测试用例中一直失败。我想不出它们可能是什么(2 个测试用例失败,但它们没有显示输出)。你能给我一些关于它们可能是什么的想法吗?
原问题:
K caterpillars are eating their way through N leaves, each caterpillar falls from leaf to leaf in a unique sequence, all caterpillars start at a twig at position 0 and falls onto the leaves at position between 1 and N. Each caterpillar j has as associated jump number Aj. A caterpillar with jump number j eats leaves at positions that are multiple of j. It will proceed in the order j, 2j, 3j…. till it reaches the end of the leaves and it stops and build its cocoon. Given a set A of K elements K<-15, N<=10^9, we need to determine the number of uneaten leaves.
Input:
N = No of uneaten leaves K = No. of caterpillars A = Array of integer jump numbers
Output:
The integer nu. Of uneaten leaves
Sample Input:
10 3 2 4 5
Output:
4
Explanation:
[2, 4, 5] is a j member jump numbers, all leaves which are multiple of 2, 4, and 5 are eaten, leaves 1,3,7,9 are left, and thus the no. 4
我的解决方案:
static int countUneatenLeaves(int N, int[] A) {
int total = 0;
for (int i = 0; i < A.length; i++) {
int multiplier = (int) Math.pow(-1, i);
total += multiplier * combination(A, i + 1, N);
}
return N - total;
}
public static int combination(int[] elements, int K, int num) {
// get the length of the array
// e.g. for {'A','B','C','D'} => N = 4
int N = elements.length;
// get the combination by index
// e.g. 01 --> AB , 23 --> CD
int combination[] = new int[K];
// position of current index
// if (r = 1) r*
// index ==> 0 | 1 | 2
// element ==> A | B | C
int r = 0;
int index = 0;
int total = 0;
while (r >= 0) {
// possible indexes for 1st position "r=0" are "0,1,2" --> "A,B,C"
// possible indexes for 2nd position "r=1" are "1,2,3" --> "B,C,D"
// for r = 0 ==> index < (4+ (0 - 2)) = 2
if (index <= (N + (r - K))) {
combination[r] = index;
// if we are at the last position print and increase the index
if (r == K - 1) {
//do something with the combination e.g. add to list or print
total += calc(combination, elements, num);
index++;
} else {
// select index for next position
index = combination[r] + 1;
r++;
}
} else {
r--;
if (r > 0) index = combination[r] + 1;
else index = combination[0] + 1;
}
}
return total;
}
private static int calc(int[] combination, int[] elements, int num) {
int eaten = 0;
if (combination.length == 1) {
eaten = (int) Math.floor(num / elements[combination[0]]);
} else {
int lcm = lcm(elements[combination[0]], elements[combination[1]]);
for (int i = 2; i < combination.length; i++) {
lcm = lcm(lcm, elements[combination[i]]);
}
eaten = Math.abs((int) Math.floor(num / lcm));
}
return eaten;
}
private static int lcm(int a, int b) {
return a * (b / findGCD(a, b));
}
private static int findGCD(int number1, int number2) {
//base case
if (number2 == 0) {
return number1;
}
return findGCD(number2, number1 % number2);
}
我自己尝试了很多测试输入,但未能找到它中断的情况。我怀疑失败的测试涉及大 N,就好像我采用简单的蛮力方法一样,相同的测试用例因超时而失败。
有什么想法吗?
最佳答案
我认为您应该使用 long to 而不是 int。因为A的元素可以更接近10^9。所以当 num gcd(a, b) 较小时,比如 1,lcm(a, b) = a * b 可能大于 0x7FFFFFFF,所以答案不正确。
关于java - 什么测试用例可以打破这个包含/排除原则挑战的解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33474644/
我想得到 id a b c -------------------- 1 1 100 90 6 2 50 100 ...来自: id a
让我们看看,我有这段将 NFA 自动转换为 DFA 的代码;这是我编写的;我发现了一个“bug”; printf()指令 这意味着像这样“printf("",X); ”以防止出现错误 没有要在屏幕上打
我有一些文本图像,但它们是弯曲的,呈圆形或波浪形。我需要把它们弄直。我尝试使用OCR提取文本,但是它们效率低下,需要直接的图像。 我附上测试图片: 我需要覆盖这两个最小区域。 请建议一些路径或使用
data1=data.frame("StudentID"=c(1,1,1,2,2,2,2,3,3,3,3), "Class"=c(1,1,1,1,1,1,1,2,2,2,2),
我的问题已在 java draw line as the mouse is moved 中提到过然而,我对这本书的了解还不够深入,无法涵盖 JPanels、JFrames 和 Points,正如提出这
这是我上一个问题 here. 的后续问题那里发布的答案实际上不起作用。所以这就是挑战。您将获得以下代码(假设包含 jQuery): $("input").val(**YOUR PHP /
以下是C语言中链表的语法,部分内容 struct tag-name { type member1; type member2; ....... ....... struc
我面临以下挑战性问题: There are a circle of 100 baskets in a room; the baskets are numbered in sequence from 1
我有一个这样的结构: public struct MyStruct { public string Name; public bool Process; } 我有一个这样的
假设我有: var directions = [ "name", "start_address", "end_address", "order_date" ]; 我正在尝试找到一种巧妙、快速的方法来将
我正在用 Javascript 重做 Project Euler 挑战。任务是获取最大的回文数( https://projecteuler.net/problem=4 )。现在我得到以下代码: var
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
第一问:有没有可能有一个不可见的矩形? 问题 2:是否可以在方法上调用方法?见下文。 var canvas = document.getElementById("canvas"); var ctx =
问题: 给定一串数字,计算是任何回文的字谜的子词(一致的子序列)的数量。 例子: 对于输入字符串“02002”,结果应该是 11,即: “0”、“2”、“0”、“0”、“2”、“00”、“020”、“
用户A-用户B-用户C-用户D-用户F 用'-'连接的用户互相认识。 我需要一个算法来完成这两项任务: 计算从UserX到UserY的路径 对于 UserX,计算距离不超过 3 步的所有用户。 有没有
根据我的教授介绍。对于数据库理论,没有任何例子可以说明这种情况何时会出现,考虑到它是理论的特定部分,这似乎有点奇怪。 我正在寻找的只是一个示例关系,它是第 4 范式并且可以执行第 5 范式分解。或者(
给定任务sameEnds来自 CodingBat: 给定一个字符串,返回出现在字符串开头和结尾且不重叠的最长子字符串。例如,sameEnds("abXab") 是 "ab"。 sameEnds("ab
在我的 welcome#index 页面上,有一个按钮可以远程(或者我应该说异步)为 Article 编写新的 Comment ),使用 AJAX。 它工作得很好,只是当使用rails迭代一篇文章时,
希望每个人都有美好的一天。 这是我在 Stackoverflow 上发表的第一篇文章! 我刚刚完成了 Codeacademy 上的 javascript 类(class),并且也阅读了几本相关书籍。现
挑战是删除数字末尾的零。两个数字内的零是可以的。例如: 14000 == 14 //all end zeros removed 10300 == 103 // all end zeros remove
我是一名优秀的程序员,十分优秀!