这题要用背包做吧~
by Ruirui_170219 @ 2018-08-25 07:50:33
@[铁达尼号80](/space/show?uid=92213) 我改了一下您的代码,您理解一下吧。看了一眼以后发现您的else语句是空的想说一下,但后来测试了一下还是不行,可能是我太蒟蒻了吧,你可以看看我帮您改的,参考一下。
```cpp
#include<bits/stdc++.h>
using namespace std;
int n,s,a,b,num,y,i,j;
struct pg
{
int h;
int l;
};
int main()
{
int ns=0;//总共需要的力气
cin>>n>>s>>a>>b;
a=a+b;
pg p[n+1];
for(i=1;i<=n;i++)
{
cin>>p[i].h>>p[i].l;
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].l>p[j].l)
{
swap(p[i].l,p[j].l);
swap(p[i].h,p[j].h);
}
}
}//冒泡排序
for(i=1;;i++)
{
if(p[i].h<=a)//判断高度
{
ns=ns+p[i].l;
if(ns<=s)//如果总共要花的力气小于等于原有力气
{
num++;
}
else
{
break;//(ns>s)
}
}
}
cout<<num;
return 0;
}
```
by adim @ 2018-08-26 17:16:07