抽签
你的朋友提议玩一个游戏:将写有数字的n个纸片放入口袋中,你可以从口袋里抽取4次纸片,每次记下纸片上的数字后都将其放回口袋中。如果这4个数字的和是m,就是你赢,否则就是你的朋友赢。你挑战了好几回,结果一次也没有赢,于是怒而撕破口袋,取出所有纸片,检查自己是否真的有赢的可能性。请你编写一个程序,判断当纸片上所写的数字是k1,k2,…,kn时,是否存在抽取4次和为m的方案。如果存在,输出Yes;否则,输出No。
限制条件:
1<<n<<1000;
1<<m<<10^8;
1<<ki<<10^8;
输入样例:
3
10
1 3 5
输出Yes(例如4次抽取的结果是1、1、3、5,和就是10)
输入样例:
3
9
1 3 5
输出No(不存在和为9的抽取方案)
代码:
#include <bits/stdc++.h>
using namespace std;
bool can(vector<int>& nums, int target) {
int n = nums.size();
unordered_map<int, int> twoSum;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int sum = nums[i] + nums[j];
twoSum[sum]++;
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int sum = nums[i] + nums[j];
int needed = target - sum;
if (twoSum.find(needed) != twoSum.end()) {
if (needed != sum || twoSum[needed] > 1) {
return true;
}
}
}
}
return false;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> nums(n);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
if (can(nums, m)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
二:对拍
#include <bits/stdc++.h>
using namespace std;
bool canFindFourNumbersSum(vector<int>& nums, int target) {
int n = nums.size();
unordered_map<int, int> twoSum;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int sum = nums[i] + nums[j];
twoSum[sum]++;
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int sum = nums[i] + nums[j];
int needed = target - sum;
if (twoSum.find(needed) != twoSum.end()) {
if (needed != sum || twoSum[needed] > 1) {
return true;
}
}
}
}
return false;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> nums(n);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
if (canFindFourNumbersSum(nums, m)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}