二分查找之变形

//寻找一个数,找到刚好比他小的数。

比如-1 1 1 1 2 ~~~

如果要找1,那么返回的是-1的位置0。

如果1开头,那么返回位置-1.

int lbs(int *a, int n, int v){
	int b = 0, e = n, m;
	while (b < e){
		m = b + ((e - b) >> 1);
		if (a[m] >= v) e = m;
		else b = m + 1;
	}
	while (0 <= m && a[m] >= v) --m;
	return m;
}

用同样的方法来找刚好比其大的数。

int rbs(int *a, int n, int v){
	int b = 0, e = n, m;
	while (b < e){
		m = b + ((e - b) >> 1);
		if (a[m] <= v) b = m + 1;
		else e = m;
	}
	while (m < n && a[m] <= v) ++m;
	return m;
}

这两个函数可以用于对在[b , e]区间里面的数进行计数。比如POJ 2413题。不过要注意数列是从 1, 2, 3开始的。不是一般的从1 1 2 3开始。

粘个java的代码。

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
	public static int leftBinarySearch(BigInteger [] w, int n, BigInteger v){
		int b = 0, e = n, m = 0, re;
		while (b < e){
			m = (b + e) >> 1;
			re = w[m].compareTo(v);
			if (re >= 0) e = m;
			else b = m + 1;
		}
		while (0 <= m && w[m].compareTo(v) >= 0) --m;
		return m;
	}
	
	public static int rightBinarySearch(BigInteger [] w, int ms, BigInteger v){
		int b = 0, e = ms, m = 0, re;
		while (b < e){
			m = b + ((e - b) >> 1);
			re = w[m].compareTo(v);
			if (re <= 0) b = m + 1;
			else e = m;
		}
		while (m < ms && w[m].compareTo(v) <= 0) ++m;
		return m;
	}
    public static void main(String[] args) throws Exception {
    	int [] a = new int[50];

    	final int ms = 500;
    	BigInteger [] ar = new BigInteger[ms];
    	ar[0] = BigInteger.ONE;
    	ar[1] = BigInteger.valueOf(2);
    	a[0] = 1; a[1] = 2; a[2] = 3;
    	for (int i = 2; i <= 42; ++i){
    		a[i] = a[i-1] + a[i-2];
    		ar[i] = BigInteger.valueOf(a[i]);
    	}
    	for (int i = 43; i < ms; ++i){
    		ar[i] = ar[i-1].add(ar[i-2]);
    	}
    	
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String line;
		while ((line = cin.readLine()) != null) {
			StringTokenizer st = new StringTokenizer(line);

			int i = 0;
			while (i < line.length()
					&& (' ' == line.charAt(i) || '0' == line.charAt(i)))
				++i;
			if (i == line.length())
				break;

			BigInteger aa = new BigInteger(st.nextToken());
			BigInteger bb = new BigInteger(st.nextToken());
			
			int from = leftBinarySearch(ar, ms, aa);
			int to = rightBinarySearch(ar, ms, bb);
			//System.out.println("" + from + ' ' + to);
			System.out.println("" + (to - from - 1));
		}
    }
}




在Matlab中,可以使用二分查找法来查找数组中的元素。以下是使用递归实现的二分查找算法的示例代码: ```matlab function BinarySearch_2(array, low, high, value) n = length(array); if low > high fprintf('Sorry, the number does not exist !!! \n\n') else mid = floor((low + high) / 2); if array(mid) == value fprintf('The number is found at position %d. \n\n', mid); elseif array(mid) < value BinarySearch_2(array, mid + 1, high, value); else BinarySearch_2(array, low, mid - 1, value); end end end % 测试数组 BinarySearch_2([1 3 4 6 8 10 13 21 23 46 54 56], 1, 12, 21); ``` 以上代码实现了一个二分查找函数`BinarySearch_2`,接受一个有序数组、数组的下界、上界和要查找的值作为输入。函数会在数组中查找指定的值,并返回它的位置。如果值不存在于数组中,会输出提示信息。 请注意,这只是一个示例代码,你可以根据自己的需求进行修改和扩展。 另外,除了二分查找,Matlab还提供了其他查找方法,比如线性查找、插值查找等。你可以根据具体情况选择合适的查找算法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [二分查找法及其四种变形(MATLAB)](https://blog.csdn.net/HuiningM/article/details/100173538)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Matlab中顺序查找、二分搜索及一二三次样条的一些总结](https://blog.csdn.net/m0_62961080/article/details/121879268)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值