poj上的搜索题一会再做,先做一道简单的~~
#include <iostream>
#include<algorithm>
using namespace std;
int k[100];
int kk[100];
int n;
bool f;
bool binary_search(int x)
{
int l = 0, r = n;
int mid;
while(l < r)
{
mid = (l+r)/2;
if(kk[mid] == x)
return true;
else if(x < kk[mid])
r = mid - 1;
else
l = mid + 1;
}
return false;
}
int main()
{
int m;
cin >> n >> m;
for(int i=0; i<n; i++)
{
cin >> k[i];
}
int a, b, c, d;
for(int c=0; c<n; c++)
{
for(int d=0; d<n; d++)
{
kk[c*n + d] = k[c] + k[d];
}
}
sort(kk,kk+n*n);
for(a=0; a<n; a++)
{
for(b=0; b<n; b++)
{
if(binary_search(m-k[a]-k[b]))
f = true;
}
}
if(f)
cout << "Yes\n";
else
cout << "No\n";
return 0;
}