kunjinkao55 @ 2024-08-15 01:12:37
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, s, sum = 0; // n: total number, s: total strength, sum: number of apples picked
int a, b; // a + b represents the height of hands
scanf("%d %d", &n, &s);
scanf("%d %d", &a, &b);
int* x;
int i;
x = (int*)malloc(n * sizeof(int));
if (x == NULL) {
return 1;
}
int* y;
y = (int*)malloc(n * sizeof(int));
if (y == NULL) {
return 1;
}
for (i = 0; i < n; i++)
{
scanf("%d %d", &x[i], &y[i]);
}
for (i = 0; i < n; i++)
{
if (a + b >= x[i] && s>=y[i] )
{
sum++;
s = s - y[i];
if (s < 0)
{
break;
}
}
}
printf("%d", sum);
free(x);
free(y);
return 0;
}
by Malkin_Moonlight @ 2024-08-15 06:35:24
@kunjinkao55 你的变量
by lxr_Galaxy @ 2024-08-15 07:09:56
这题他的s是输入的,没赋初值应该问题不大((逃@guanhetian
by Malkin_Moonlight @ 2024-08-15 07:57:38
@lxr_Galaxy 哦,抱歉,我看错了,感谢大佬指正!
by tan1220 @ 2024-08-16 10:34:16
@kunjinkao55 直接按力气排序 再看够不够高
#include<bits/stdc++.h>
using namespace std;
int c[5001],h=0,d[5001];
int main(){
int n,s,x,y,b,a,cd;
cin>>n>>s;
cin>>a>>b;
cd=b+a;
for(int i=1;i<=n;i++){
cin>>x>>y;
c[i]=y;
d[i]=x;
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(c[i]>c[j]){
swap(c[i],c[j]);
swap(d[i],d[j]);
}
for(int i=1;i<=n;i++){
if(cd>=d[i]&&s-c[i]>=0){
h++;
s-=c[i];
}
}
cout<<h;
return 0;
}