| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
高手帮忙看看哪里错了?#include <iostream>
using namespace std;
int a[51];
int f(int a[],int n)
{
int x;
if(n == 2)
{
return a[1];
}
if(n == 3)
{
return a[0]+a[1]+a[2];
}
if(n > 3)
{
//两种可能,一种是最小和次小分别带最大和次大,一种是最小的分别带过最大的和次大的,取最优的
if( (2*a[1]+a[n-1]+a[0]) < (2*a[0]+a[n-2]+a[n-1]))
{
x = (2*a[1]+a[n-1]+a[0]);
}
else
{
x = (2*a[0]+a[n-2]+a[n-1]);
}
return f(a,n-2) + x ;
}
return 0;
}
int main()
{
int n,i,j,temp;
cin>>n;
for(i=0; i<n; i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
cout<<f(a,n)<<endl;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator