题意:
你的朋友提议玩一个游戏:将写有数字的n个纸片放入口袋中,你可以从口袋中抽取4次纸片,每次记下纸片上的数字后都将其放回口袋中。如果这4个数字的和是m,就是你赢,否则就是你的朋友赢。你挑战了好几回,结果一次也没赢过,于是怒而撕破口袋,取出所有纸片,检查自己是否真的有赢的可能性。请你编写一个程序,判断当纸片上所写的数字是k1, k2, …, kn时,是否存在抽取4次和为m的方案。如果存在,输出Yes;否则,输出No。(1<=n<=1000,1<=m<=1e8,1<=k<=1e8)
解法1:暴力
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1005;
const int INF = 2100000000;
int n, m, k[MAXN];
void solve()
{
bool flag = false;
for(int a = 0; a < n; a++)
{
for(int b = 0; b < n; b++)
{
for(int c = 0; c < n; c++)
{
for(int d = 0; d < n; d++)
{
if(k[a] + k[b] + k[c] + k[d] == m)
{
flag = true;break;
}
}
}
}
}
if(flag) puts("yes");
else puts("no");
}
int main()
{
freopen("input.txt", "r", stdin);
while(~scanf("%d", &n))
{
scanf("%d", &m);
for(int i = 0; i < n; i++)
scanf("%d", &k[i]);
solve();
}
return 0;
}
解法2:稍加优化
//@auther yangZongJun
/********************************************//**
Date : 2015
题目来源:
题 意:
解题思路:
***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1005;
const int INF = 2100000000;
int n, m, k[MAXN];
/*
void solve()
{
bool flag = false;
for(int a = 0; a < n; a++)
{
for(int b = 0; b < n; b++)
{
for(int c = 0; c < n; c++)
{
for(int d = 0; d < n; d++)
{
if(k[a] + k[b] + k[c] + k[d] == m)
{
flag = true;break;
}
}
}
}
}
if(flag) puts("yes");
else puts("no");
}
*/
bool binary_search(int x)
{
int l = 0, r = n;
while(r - l >= 1)
{
int mid = (r + l) / 2;
if(k[mid] == x) return true;
else if(k[mid] < x) l = mid + 1;
else r = mid;
}
return false;
}
void solve()
{
sort(k, k+n);
bool flag = false;
for(int a = 0; a < n; a++)
{
for(int b = 0; b < n; b++)
{
for(int c = 0; c < n; c++)
{
if(binary_search(m - k[a] - k[b] - k[c]))
flag = true;
}
}
}
if(flag) puts("yes");
else puts("no");
}
int main()
{
freopen("input.txt", "r", stdin);
while(~scanf("%d", &n))
{
scanf("%d", &m);
for(int i = 0; i < n; i++)
scanf("%d", &k[i]);
solve();
}
return 0;
}
解法三:最优
//@auther yangZongJun
/********************************************//**
Date : 2015
题目来源:
题 意:
解题思路:
***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1005;
const int INF = 2100000000;
int n, m, k[MAXN];
int kk[MAXN*MAXN];
/*
void solve()
{
bool flag = false;
for(int a = 0; a < n; a++)
{
for(int b = 0; b < n; b++)
{
for(int c = 0; c < n; c++)
{
for(int d = 0; d < n; d++)
{
if(k[a] + k[b] + k[c] + k[d] == m)
{
flag = true;break;
}
}
}
}
}
if(flag) puts("yes");
else puts("no");
}
*/
/*
bool binary_search(int x)
{
int l = 0, r = n;
while(r - l >= 1)
{
int mid = (r + l) / 2;
if(k[mid] == x) return true;
else if(k[mid] < x) l = mid + 1;
else r = mid;
}
return false;
}
void solve()
{
sort(k, k+n);
bool flag = false;
for(int a = 0; a < n; a++)
{
for(int b = 0; b < n; b++)
{
for(int c = 0; c < n; c++)
{
if(binary_search(m - k[a] - k[b] - k[c]))
flag = true;
}
}
}
if(flag) puts("yes");
else puts("no");
}
*/
bool binary_search(int x)
{
int l = 0, r = n*n;
while(r - l >= 1)
{
int mid = (r + l) / 2;
if(kk[mid] == x) return true;
else if(kk[mid] < x) l = mid + 1;
else r = mid;
}
return false;
}
void solve()
{
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);
bool flag = false;
for(int a = 0; a < n; a++)
{
for(int b = 0; b < n; b++)
{
if(binary_search(m - k[a] - k[b]))
flag = true;
}
}
if(flag) puts("yes");
else puts("no");
}
int main()
{
freopen("input.txt", "r", stdin);
while(~scanf("%d", &n))
{
scanf("%d", &m);
for(int i = 0; i < n; i++)
scanf("%d", &k[i]);
solve();
}
return 0;
}