作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用普通数组制作一个幸运数字程序,以便列表中的其余数字不会向前移动。我遵循的模式是 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93 ,99,...了解更多信息:Lucky Numbers
这是我制作的程序:
public class LuckyNumbers {
public static void main(String[] args) {
int[] lucky = new int[101];
for (int a = 0; a < lucky.length; a++){
lucky[a] = a;
}
//goes through each element in list
for (int b = 2; b < lucky.length; b++){
//checks if number is surviving
if (lucky[b] != 0){
/* if it does survive, go through the list deleting elements
* (setting them to zero) that fall on the
* index of the multiples of the the surviving number*/
int luckyNum = lucky[b]; // so that the number doesn't change
for (int c = 1; c < lucky.length;c++){
int d = luckyNum * c;
if (d < lucky.length){
lucky[d] = 0;
continue;
}
}
}
}
for (int f = 0; f < lucky.length; f++){
if (lucky[f] != 0){
System.out.println(lucky[f]);
}
}
}
}
输出是1。我认为这是一个逻辑错误。
最佳答案
问题出在代码的这一部分:
for (int c = 1; c < lucky.length;c++){
int d = luckyNum * c;
if (d < lucky.length){
lucky[d] = 0;
continue;
}
}
当您查看 wiki 页面时,您必须删除所有第 c 个幸存数字。你正在消除每一个倍数。因此,对于数字 3,您应该消除 5, 11, 17 ...
而不是 3, 6, 9...
您现在正在做的事情。
关于java - 幸运数字程序未显示正确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27754892/
提示:幸运数字是数字为7且数字中#7的个数必须为奇数。编写一个递归函数来检查一个数字是否幸运。 (例如,幸运#:777、117、7。不是幸运#:77、1277)。 我是初学者。我写了下面的代码,但我不
在我的 C++ 程序中,我有一个函数返回一个包含元素的映射,每个元素都可以有一个指向映射中另一个元素的指针。我在函数末尾返回 map 之前设置了这些指针。示例代码: #include #includ
我是一名优秀的程序员,十分优秀!