查找算法

并行搜素

#include<iostream>
#include <stdio.h>
#include <iostream>
#include <time.h>

using namespace std;

#define TEST_SIZE (1024*1024*200)
#define NUMBER 20
typedef struct _search {
	int *data;//搜索的数据集
	size_t start; //搜索的开始位置
	size_t end; //搜索的终止位置
	size_t count; //搜索结果
}search;



int main()
{



	system("pause");
	return 0;
}

二分查找

#include<iostream>

using namespace std;

template<class T>
int BinarySearch(T *list, const int n, const T x)
{
	int low, high, mid;
	low = 0;
	high = n - 1;
	while (low <= high)
	{
		mid = (low + high) / 2;
		if (x == list[mid])
		{
			return mid;
		}
		else if (x > list[mid])
		{
			low = mid + 1;
		}
		else if (x < list[mid])
		{
			high = mid - 1;
		}
	}
	return -1;

}
//递归实现
int BinarySearch_R(int *list, const int left, const int right, const int x)
{
	if (left <= right)
	{
		int mid = (left + right) / 2;
		if (x < list[mid])
		{
			return BinarySearch_R(list, left, mid - 1, x);
		}
		else if (x > list[mid])
		{
			return BinarySearch_R(list, mid + 1, right, x);
		}
		else
		{
			return mid;
		}
	}
	return -1;
}

int main()
{
	int n[] = { 1,3,5,7,9,11,13 };

	int num1 = BinarySearch(n, size(n), 11);
	int num2 = BinarySearch_R(n, 0, size(n), 11);

	cout << num1 << endl;
	cout << num2 << endl;


	system("pause");
	return 0;
}

穷举搜索

#include<iostream>

using namespace std;

//列举所有可能的情况,逐个判断有哪些是符合问题所要求的条件,从而得到问题的全部解答

int main()
{
	int a100 = 0;	//1元
	int a50 = 0;	//5角
	int a10 = 0;	//1角
	int a5 = 0;		//5分
	int cnt = 0;	//可行方案数

	for (a100 = 0; a100 <= 10; a100++)
	{
		for (a50 = 0; a50 <= 20; a50++)
		{
			for (a10 = 0; a10 <= 20; a10++)
			{
				for (a5 = 0; a5 <= 20; a5++)
				{
					if ((a100 * 100 + a50 * 50 + a10 * 10 + a5 * 5) == 1000 && (a100
						+ a50 + a10 + a5) == 20)
					{
						cout << a100 << " , " << a50 << " , " << a10 << " , " << a5 << endl;
						cnt++;
					}
				}
			}
		}
	}
	cout << "可行的解决方案总共有:" << cnt << endl;

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值