额,花了3天调试,终于AC了:
#include <stdio.h>
#include <stdlib.h>
FILE *fin,*fout;
void Qsort(int fruit[],int startPos, int endPos)
{
int i,j,temp;
temp=fruit[startPos];
i=startPos; j=endPos;
while(i<j)
{
while(fruit[j]<=temp && i<j)--j;
fruit[i]=fruit[j];
while(fruit[i]>=temp && i<j)++i;
fruit[j]=fruit[i];
}
fruit[i]=temp;
if(i-1>startPos) Qsort(fruit,startPos,i-1);
if(endPos>i+1) Qsort(fruit,i+1,endPos);
}
int main()
{
int fruit[10000]={0},n,i,ans=0,temp=0,j=0,k=0;
fin=fopen("fruit.in","r");
fout=fopen("fruit.out","w");
fscanf(fin,"%d",&n);
for(i=0;i<n;i++)
fscanf(fin,"%d",&fruit[i]);
Qsort(fruit,0,n-1);
for(i=n-1;i>0;i--)
{
temp=fruit[i]+fruit[i-1];
ans=ans+temp;
j=0;
while(fruit[j]>temp)j=j+1;
for(k=n-1;k>=j;k--)
fruit[k+1]=fruit[k];
fruit[j]=temp;
}
fprintf(fout,"%d",ans);
system("pause");
return 0;
}