谁给我编几道题啊用c++语言 谢谢了

TheA+BProblemseemsalittleeasy,nowweturntoanenhancedversion...Givenaseriesofnumbers,yo... The A+B Problem seems a little easy, now we turn to an enhanced version...

Given a series of numbers, your task is to pick up some numbers, so that the sum of these numbers is a given S.

Input

The first line of each test contains the amount of the numbers N (1 ≤ N ≤ 20), and the sum S. Then N integers followed in the second line seperated by spaces. You can assume the sum S and all the N numbers are in the range [-10000, 10000].

The input is terminated by a line with N = S = 0.

Output

Output one line for each test case. If you can pick up some numbers whose sum is S, just output "Yes". Otherwise, output "No".

Sample Input

3 4
1 2 3
4 10
1 2 3 -4
0 0

Sample Output

Yes
No

Author: RoBa
我明天需要交 麻烦高手了 高手的话 2分钟就ok了 谢了
展开
 我来答
zhuyifei250
2010-08-29 · TA获得超过133个赞
知道答主
回答量:108
采纳率:0%
帮助的人:89.7万
展开全部
望指正
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool find(vector<int>a,int x);

int main(void)
{
int N,sum;
cin>>N>>sum;
vector<int>num;
while (!(N==0&&sum==0))
{
int temp;
int i;
num.clear();
for (i=0;i<N;i++)
{
cin>>temp;
num.push_back(temp);
}
sort(num.begin(),num.end());
for (i=0;i<N/2;i++)
{
if (find(num,sum-num[i]))
{
cout<<"YES"<<endl;
break;
}
}
if (i==N/2)
{
cout<<"NO"<<endl;
}
cin>>N>>sum;

}
return 0;

}

bool find(vector<int>a,int x)
{
int low=0;
int high=a.size()-1;
int middle;
while(low<=high)
{
middle=(low+high)/2;
if (a[middle]>x)
{
high=middle-1;
}
else if (a[middle]==x)
{
return true;
}else
{
low=middle+1;
}
}
return false;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式