Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

高手帮忙看看哪里错了?

Posted by swust20051578 at 2007-09-29 21:31:18 on Problem 3404
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator