- 合并果子
- 2016-11-17 21:37:24 @
为什么用sort函数只得一半而手打快排就得满了?
include<iostream> include<algorithm>
using namespace std;
bool cmp(int x,int y)
{
return x<y; } int main() { int n,i,j,count=0,a[10001]; cin>>n;
for(i=0;i<n;++i) cin>>a[i];
sort(a,a+n-1,cmp);
for(i=0;i<n-1;++i) { count+=a[i]+a[i+1]; a[i+1]=a[i]+a[i+1]; a[i]=0; for(j=i+1;j<n-1;++j) if (a[j]>a[j + 1])
swap(a[j],a[j + 1]);
else break;
}
cout<<count;
return 0;
}
2 条评论
-
贱人在我右边 LV 9 @ 2016-12-14 15:10:25
shi
-
2016-11-18 08:27:29@
sort(a,a+n,cmp);
- 1