I_am_zcr @ 2024-11-23 10:10:00
原题 10分,求调
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int l,n[105],m,ans=0;
cin>>l;
for(int i=0;i<l;i++){
cin>>n[i];
}
cin>>m;
sort(n,n+l);
for(int i=0;i<l;i++){
if(m>n[i]){
ans++;
m-=n[i];
}
}
cout<<ans;
return 0;
}
by I_am_zcr @ 2024-11-23 10:12:10
刚刚将“if(m>n[i])”改成了“if(m>=n[i]”还是不行
by yinzixia @ 2024-11-23 10:18:38
@I_am_zcr 为什么要sort呢?
by yinzixia @ 2024-11-23 10:21:11
@I_am_zcr "小明先后见到想买的物品"那顺序不是固定的吗?
by I_am_zcr @ 2024-11-23 10:21:30
@yinzixia从小到大的价格买的东西应该更多吧
by yinzixia @ 2024-11-23 10:22:32
@I_am_zcr 不能if(m>n[i]),要if(m>=n[i]),然后去掉sort
by yinzixia @ 2024-11-23 10:23:10
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int l,n[105],m,ans=0;
cin>>l;
for(int i=0;i<l;i++){
cin>>n[i];
}
cin>>m;
for(int i=0;i<l;i++){
if(m>=n[i]){
ans++;
m-=n[i];
}
}
cout<<ans;
return 0;
}
@I_am_zcr AC
by libohan356218 @ 2024-11-23 10:23:27
@I_am_zcr
#include <bits/stdc++.h>
using namespace std;
int l,n[105],m,ans=0;
int main(){
cin>>l;
for(int i=0;i<l;i++){
cin>>n[i];
}
cin>>m;
for(int i = 0;i<l;i++){
if(m>=n[i]){
ans++;
m-=n[i];
}
}
cout<<ans;
return 0;
}
by yinzixia @ 2024-11-23 10:24:40
@I_am_zcr 可它顺序只能是固定的
by I_am_zcr @ 2024-11-23 10:25:08
谢谢各位,AC了
by libohan356218 @ 2024-11-23 10:26:55
@I_am_zcr
##### 求关
希望更丰富的展现?阅读 Markdown 使用帮助、KaTeX 文档。