结构体编译不过是为什么

P1478 陶陶摘苹果(升级版)

王宏宇11 @ 2019-02-23 18:23:40

一枚真实的萌新,不会用结构体,代码结构体编译不过,不知道为什么,望大佬们看看

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct  node
{int xi;
 int yi;//力气 
};
node a[5005];
int main()
{int n,s,h,b;
int sum=0;
cin>>n>>s;
cin>>h>>b;
for(int i=1;i<=n;i++)
   cin>>a[i].xi>>a[i].yi;
sort(a+1,a+1+n);//排序 
while(s>=0)
   {int i=1;
    if(h+b>=a[i].xi)
      {sum++;
       s=s-a[i].yi;//如果可以就减去力气 
      } 
    i++;
    if(i>n)//力气多了退出 
     break;
    }
cout<<sum;
return 0;
} 

by csdfret @ 2019-02-23 19:45:40

@王宏宇11 s的最后结果会小于零


by 王宏宇11 @ 2019-02-23 19:47:51

@   所以循环的条件不应该是

while(s>=0)


by csdfret @ 2019-02-23 19:59:44


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct  node
{int xi;
 int yi;//力气 
};
bool c(node x,node y)
{
  return x.yi<y.yi;
}
node a[5005];
int main()
{int n,s,h,b;
int sum=0;
cin>>n>>s;
cin>>h>>b;
for(int i=1;i<=n;i++)
   cin>>a[i].xi>>a[i].yi;
sort(a+1,a+1+n,c);//排序 
int i=1;
while(s>=0)
   {
   if(i>n)//力气多了退出 
     break;
    if(h+b>=a[i].xi&&s-a[i].yi>=0)
      {
       sum++;
       s=s-a[i].yi;//如果可以就减去力气 
      } 
    i++;
    }
cout<<sum;
return 0;
} 

by csdfret @ 2019-02-23 20:00:30

@王宏宇11


by 王宏宇11 @ 2019-02-23 20:02:03

@   明白了,实在是麻烦您了


上一页 |