简体   繁体   English

在数组中查找周围的数字(java)

[英]Finding surrounding numbers in array (java)

My question is related to the fibonacci sequence of numbers (but for simplicity, you could apply it to things like square/prime numbers etc) 我的问题与斐波那契数列有关(但为简单起见,您可以将其应用于平方/素数等)

long f1 = 0;
long f2 = 1;
long fibonacci = 0;
long[] fibonaccinumbers = new long[52];
fibonaccinumbers[0]=0;
fibonaccinumbers[1]=1;
for(int count = 2; count<=51; count++)
{
    fibonacci = f2+f1;
    fibonaccinumbers[count] = fibonacci;
    f1 = f2;
    f2 = fibonacci;        
}

The above code generates the array for fibonacci numbers 0-51. 上面的代码生成斐波那契数字0-51的数组。

Now what I'm looking to do is enter a number, we'll use 30 as an example. 现在我要输入的是数字,我们以30为例。 and then find and display the numbers before and after it in the sequence, which would be 21 and 34. 然后找到并显示序列前后的数字,分别是21和34。

What is tripping me up is getting into the array and searching above and below my given number to find a match. 使我不寒而栗的是,进入数组并在给定数字的上方和下方搜索以找到匹配项。 How could I do this? 我该怎么办?

Since the Fibonaccy series is a sorted array (having ascending order), you can use int index = Arrays.binarySearch(fibonaccinumbers,30); 由于Fibonaccy系列是排序数组(具有升序),因此可以使用int index = Arrays.binarySearch(fibonaccinumbers,30); to get the index such that fibonaccinumbers[index-1] < 30 < fibonaccinumbers[index] . 获取索引,使fibonaccinumbers[index-1] < 30 < fibonaccinumbers[index]

Therefore fibonaccinumbers[index-1] will contain 21 and fibonaccinumbers[index] will contain 34. 因此, fibonaccinumbers[index-1]将包含21,而fibonaccinumbers[index]将包含34。

Note that Arrays.binarySearch will return fibonaccinumbers.length if all the numbers in your array are smaller than the number you are searching for. 请注意,如果数组中的所有数字均小于要搜索的数字,则Arrays.binarySearch将返回fibonaccinumbers.length

This should be very simple, you just need to access the array at +1 and -1 of the index you have entered, for example, if the user enters 30... 这应该非常简单,您只需要访问输入索引的+1和-1处的数组,例如,如果用户输入30 ...

System.out.println("BEFORE: " + fibonaccinumbers[input-1]);
System.out.println("AFTER: " + fibonaccinumbers[input+1]);

Where input is what the user enters, so in the case, input-1 would be 29. 输入是用户输入的内容,因此在这种情况下,input-1将为29。

Just be sure to make sure input-1 and input+1 do not go out of the bounds od your array. 只要确保输入1和输入1不会超出数组的范围即可。

If you wanted to find out what the actual fibonnaci number is as opposed to its number in the sequence, you will need to do a search for this first and then use the index you find to again, find what is before and after. 如果您想找出实际的斐波那契编号与其序列中的编号相对应,则需要先搜索该编号,然后再次使用找到的索引来查找之前和之后的编号。

If you´d don´t want to use the binary search provided by the Arrays class then you could simply loop over the array and check for the needed values. 如果您不想使用Arrays类提供的二进制搜索,则可以简单地遍历数组并检查所需的值。 You just need an int value that indicates the index in the array. 您只需要一个int值即可指示数组中的索引。

public static void main(String[] args) {
    long[] fibs = createFib();
    long search = 30;
    int small = 0;
    for(long i : fibs) {
        if(i < search) {
            ++small;
        } else {
            break;
        }
    }
    if(small < fibs.length)
        System.out.println(fibs[small-1] + " and " + fibs[small] + " do surround the value " + search);
    else {
        System.out.println(" the value " + search +" is to high, only " + fibs[small-1] + " is in range");
    }
}

private static long[] createFib() {
    long f1 = 0;
    long f2 = 1;
    long fibonacci = 0;
    long[] fibonaccinumbers = new long[52];
    fibonaccinumbers[0]=0;
    fibonaccinumbers[1]=1;
    for(int count = 2; count<=51; count++)
    {
        fibonacci = f2+f1;
        fibonaccinumbers[count] = fibonacci;
        f1 = f2;
        f2 = fibonacci;        
    }
    return fibonaccinumbers;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM