萌新比赛时只拿到 260pts,太菜了,这题还挂了 10pts。
这份代码,即我赛时的代码,只拿到了 90pts。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n,w;
int s[N];
inline int read(){
int ret = 0;
char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)){
ret=ret * 10 + c - '0';
c = getchar();
}
return ret;
}
signed main(){
n = read();
for(int i=0;i<n;++i) s[i] = read();
sort(s,s+n);
w = read();
int t = 0;
for(int i=0;i<n;++i){
if(s[i]<=w) t=i;
else break;
}
w = s[t];
int ans = 0;
for(int i=0;i<t;++i){
if(w>=s[i]) w -= s[i],ans++;
else break;
}
cout<<ans;
return 0;
}
但是,下面这份代码仅仅是改了一下下标,就A了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n,w;
int s[N];
inline int read(){
int ret = 0;
char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)){
ret=ret * 10 + c - '0';
c = getchar();
}
return ret;
}
signed main(){
n = read();
for(int i=1;i<=n;++i) s[i] = read();
sort(s+1,s+n+1);
w = read();
int t = 0;
for(int i=1;i<=n;++i){
if(s[i]<=w) t=i;
else break;
}
w = s[t];
int ans = 0;
for(int i=1;i<=t;++i){
if(w>=s[i]) w -= s[i],ans++;
else break;
}
cout<<ans;
return 0;
}
求助各位,两份代码有何不同?