P1478 陶陶摘苹果(升级版)

测试数据
8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2

#include<bits/stdc++.h>
using namespace std;
struct node{
 int x;
 int y;
}f[5050];
bool cmp(const node &n1,const node &n2)
{
 return n1.y-n2.y;//用减排序失败
}
int main()
{
 int n,s,a,b,i,j;
 scanf("%d%d",&n,&s);
 scanf("%d%d",&a,&b);
 memset(f,0,sizeof(f));
 for(i=0;i<n;i++)
 {
  scanf("%d%d",&f[i].x,&f[i].y);
  if(f[i].x>(a+b))
  {
   f[i].y=200;
  }
 }
 printf("----------排序前--------------------------\n");
 for(i=0;i<n;i++)
 {
  printf("%d  %d\n",f[i].x,f[i].y);
 }
 printf("------------------------------------\n");
 sort(f,f+n,cmp);
 int ans=0;
 printf("------------排序后------------------------\n");
 for(i=0;i<n;i++)
 {
  printf("%d  %d\n",f[i].x,f[i].y);
  if(s-f[i].y>=0 && f[i].y!=200)
  {
   s-=f[i].y;
   ans++;
  }
//  else
//  {
//   printf("%d   %d",s,f[i].y);
////   break;
//  }
//   
 }
 printf("------------------------------------\n");
 printf("%d",ans);
 return 0;
}



#include<bits/stdc++.h>
using namespace std;
struct node{
 int x;
 int y;
}f[5050];
bool cmp(const node &n1,const node &n2)
{
  return n1.y<n2.y;//更改为这个之后排序成功   不明白知道是什么原因
}
int main()
{
 int n,s,a,b,i,j;
 scanf("%d%d",&n,&s);
 scanf("%d%d",&a,&b);
 memset(f,0,sizeof(f));
 for(i=0;i<n;i++)
 {
  scanf("%d%d",&f[i].x,&f[i].y);
  if(f[i].x>(a+b))
  {
   f[i].y=200;
  }
 }
 printf("----------排序前--------------------------\n");
 for(i=0;i<n;i++)
 {
  printf("%d  %d\n",f[i].x,f[i].y);
 }
 printf("------------------------------------\n");
 sort(f,f+n,cmp);
 int ans=0;
 printf("------------排序后------------------------\n");
 for(i=0;i<n;i++)
 {
  printf("%d  %d\n",f[i].x,f[i].y);
  if(s-f[i].y>=0 && f[i].y!=200)
  {
   s-=f[i].y;
   ans++;
  }
//  else
//  {
//   printf("%d   %d",s,f[i].y);
////   break;
//  }
//   
 }
 printf("------------------------------------\n");
 printf("%d",ans);
 return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36734025/article/details/80028510