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

求救:怎么老是WA啊???

Posted by LoyaltyJi at 2005-08-29 23:34:19 on Problem 2497
#include <iostream.h>

int i;

void swap1(int *a,int n);
void swap2(int *a,int n);

int main()
{
	int time,n,j = 1;
	int cases;
	cin >> cases;

	while(cases--)
	{
		cin >> time >> n;
		int sum,score;
		int * a,b[3][2];
		a = new int[n];

		for(i = 0;i < n;i++)
			cin >> a[i];

		i = 0,sum = 0,score = 0;	
		while(sum < time) 
		{
			if(i < n)
			{
				sum += a[i++];
			    score += sum;
			}
			else if(i == n)
				break;
		}
		if(i == n && sum < time)       
		{
			b[0][0]	= i;
		    b[0][1] = score;
		}
		else if(i == n && sum >time)
		{ 
			b[0][0]	= --i;			  
		    b[0][1] = score - sum;
		}
		else
		{
			b[0][0] = i;
			b[0][1]	= score;
		}
		
		swap1(a,n);					   
		i = 0,sum = 0,score = 0;
		while(sum < time) 
		{
			if(i < n)
			{
				sum += a[i++];
			    score += sum;
			}
			else if(i == n)
				break;
		}
		if(i == n && sum < time)
		{
			b[1][0]	= i;
		    b[1][1] = score;
		}
		else if(i == n && sum > time)
		{  
			b[1][0]	= --i;
		    b[1][1] = score - sum;
		}
		else
		{
			b[1][0] = i;
			b[1][1]	= score;
		}
		
		swap2(a,n);						 
		i = 0,sum = 0,score = 0;
		while(sum < time) 
		{
			if(i < n)
			{
				sum += a[i++];
			    score += sum;
			}
			else if(i == n)
				break;
		}
		if(i == n && sum < time)
		{
			b[2][0]	= i;
		    b[2][1] = score;
		}
		else if(i == n && sum > time)
		{  															  
			b[2][0]	= --i;
		    b[2][1] = score - sum;
		}
		else 
		{
			b[2][0] = i;
			b[2][1] = score;
		}
		
		cout << "Scenario #" << j++ << ":" << endl;
		if(b[0][0] > b[1][0] && b[0][0] > b[2][0] && b[0][1] < b[1][1] && b[0][1] < b[2][1])
		{
			cout << "Bill wins with " << b[0][0];
		    cout << " solved problems and a score of ";
			cout << b[0][1] << "." << endl << endl;
		}
		else if(b[1][0] > b[0][0] && b[1][0] > b[2][0] && b[1][1] < b[0][1] && b[1][1] < b[2][1])
		{
			cout << "Steve wins with " << b[1][0];
		    cout << " solved problems and a score of ";
			cout << b[1][1] << "." << endl << endl;
		}
		else if(b[2][0] > b[0][0] && b[2][0] > b[1][0] && b[2][1] < b[0][1] && b[2][1] < b[1][1])
		{
			cout << "Linus wins with " << b[2][0];
		    cout << " solved problems and a score of ";
			cout << b[2][1] << "." << endl << endl;
		}
		else
		{
			cout << "Steve wins with " << b[1][0];
		    cout << " solved problems and a score of ";
			cout << b[1][1] << "." << endl << endl;
		}

		delete []a;
	}

	return 0;
}

void swap1(int *a,int n)	 	
{
	int t,k;
	for(i = 0;i < n - 1;i++)
		for(k = i + 1;k < n;k++)
			if(a[i] > a[k])
			{ 
				t = a[i];
				a[i] = a[k];
				a[k] = t;
			}
}
void swap2(int *a,int n)	 
{
	int t,k;
	for(i = 0;i < n - 1;i++)
		for(k = i + 1;k < n;k++)
			if(a[i] < a[k])
			{ 
				t = a[i];
				a[i] = a[k];
				a[k] = t;
			}
}

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