Description
在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k。
Input
第一行输入序列的长度n和k,用空格分开。第二行输入序列中的n个整数,用空格分开。
Output
如果存在某两个元素的和为k,则输出yes,否则输出no。
Sample Input 1
9 10 1 2 3 4 5 6 7 8 9
Sample Output 1
yes
思路:这道题很简单,如果我们想找到和为k的两个数,无外乎找一种方法遍历所有的组合,我是直接套用的冒泡的模板
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
int n, k;
int s[1001];
bool judge = 0;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> s[i];
for(int i=0;i<n-1;i++)
for (int j = i + 1; j < n; j++)
{
if (s[i] + s[j] == k) {
judge = true;
cout << "yes";
return 0;
}
}
if (judge == 0)
cout << "no";
}