小编Ton*_*hen的帖子

在java中我想用for循环从整数数组中拉出奇数位置的每个数字.我犯了什么错误?

所以这是我从整数数组中取出的方法(这个数组的长度是偶数).

 public static int[] oddPosition (int[] array) {
    int [] oddNumbers = new int[array.length / 2];
    for (int i = 0; i < array.length; i++) {
      int j = 0;
      //System.out.println(i);//test
      //printArray(oddNumbers); //test
      if ((i + 1) % 2 != 0) {
        //System.out.println(i);//test
        oddNumbers[j] = array[i];
        j ++;
      }
    }
    return oddNumbers;
  }
Run Code Online (Sandbox Code Playgroud)

它没有用.我尝试oddNumbers在每个循环中打印出我的新数组中的情况来调试此方法.我用作1 2 8 4 6 9此方法的参数.我发现一个非常奇怪的条件是,when i = 0, (i + 1) % 2 != 0, then array[0](which is 1) should be …

java arrays algorithm for-loop

1
推荐指数
1
解决办法
61
查看次数

为什么我的小蟒蛇斐波那契探测器失败了?

出于某种原因,我必须确定一个大数字是否是斐波纳契数,所以我从互联网复制一些代码并稍微修改它,当它是大输入时似乎运行不好.这是代码:

# python program to check if x is a perfect square

import math

# A utility function that returns true if x is perfect square
def isPerfectSquare(x):
    s = int(math.sqrt(x))
    boo = (s*s == x);
    return boo

# Returns true if n is a Fibinacci Number, else false
def isFibonacci(n):

    # n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both
    # is a perferct square
    b = 5*n*n+4;
    c = 5*n*n-4;
    return isPerfectSquare(b) …
Run Code Online (Sandbox Code Playgroud)

python algorithm fibonacci

1
推荐指数
1
解决办法
128
查看次数

标签 统计

algorithm ×2

arrays ×1

fibonacci ×1

for-loop ×1

java ×1

python ×1