并行搜素
#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;
int a50 = 0;
int a10 = 0;
int a5 = 0;
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;
}