简单易懂,新手可看
2024-07-30 13:20:46
发布于:浙江
7阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a > b;
}
int main(){
int n,a[1005],cnt1 = 0,cnt2 = 0,t1 = 2,t2 = 0;
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
sort(a+1,a+n+1,cmp);
for(int i = 1;i <= n;i++){
if(i == 1) cnt1 += a[i];
else{
t2++;
if(t1 == 1) cnt1 += a[i];
else cnt2 += a[i];
if(t2 == 2){
t2 = 0;
if(t1 == 1) t1 = 2;
else t1 = 1;
}
}
}
if(cnt1 > cnt2) cout << cnt1 - cnt2;
else cout << cnt2 - cnt1;
}
这里空空如也
有帮助,赞一个