Cauchy_Mars @ 2018-03-27 16:45:11
// // main.c // P1478 陶陶摘苹果(升级版) // // Created by Mac on 13/03/2018. // Copyright © 2018 Mac. All rights reserved. //
void lineup(int ar[],int br[]); void qualified(int ar[],int br[],int arr[],int brr[]); int xi[5002],yi[5002],xii[5002],yii[5002]; int n,s,a,b,result = 0,sum = 0,length = 0; int main() { int i,j,k; scanf("%d%d%d%d",&n,&s,&a,&b); for (i = 0;i<n;i++) { scanf("%d",&xi[i]); scanf("%d",&yi[i]); } memset(xii,-1,5002); memset(yii,-1,5002); //完成了所有数据的读入 qualified(xi,yi,xii,yii); lineup(xii,yii); //进行筛选和排序 for (k = 0;k<n;k++) { if (yii[k] != -1) length++; else break; } //总共可供摘取的苹果有多少个 for (j = 0;j<length;j++) { sum+=yii[j]; result++; if (sum>s) { result--; break; } }//边加边比较
printf("%d\n",result);
return 0;
}
void qualified(int ar[],int br[],int arr[],int brr[]) { int p,q = 0; for (p = 0;p<n;p++) { if (ar[p]<=a+b) { arr[q] = ar[p]; brr[q] = br[p]; q++; } }
}//用于筛选陶陶能够摘到的苹果
void lineup(int ar[],int br[]) { int r,s; for (r = 0;r<n-1;r++) { for (s = 0;s<n-1;s++) { if (br[s]>br[s+1] && br[s+1]>=0) { int temp2 = br[s],temp1 = ar[s]; br[s] = br[s+1]; br[s+1] = temp2;
ar[s] = ar[s+1];
ar[s+1] = temp1;
}
}
}
}//将能够摘到的苹果按照力气的升序排列,使用冒泡排序法
by SofanHe @ 2018-03-27 17:20:52
@码农村村长 右上角从右向左第五个插入代码,会让你的代码整洁