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 linbzh at 2009-12-15 00:14:03 on Problem 1416
#include<iostream>
#include<fstream>
using namespace std;


int max_=0;
int *a;
bool *flag;
int *ans;
int best=0;

void solve(int n,int idx);
void input()
{
	int m;
	fstream in;
	in.open("test.txt");
	while(in>>max_)/**/
	//while(cin>>max_)
	{
		in>>m;//c
		if(m==0 && max_==0)
		{
			break;
		}
		if(m == max_)
		{
			cout<<m<<" "<<max_<<endl;
			continue;
		}
		int tmp[25];
		int cnt = 0;
		while(m>0)
		{
			int i = m%10;
			tmp[cnt++] = i;
			m = m/10;
		}
		a = new int[cnt];
		for(int k=cnt-1; k>=0; k--)
		{
			a[cnt-k-1] = tmp[k];
		}
		ans = new int[cnt];
		memset(ans, -1, sizeof(int)*cnt);
		flag = new bool[cnt];
		memset(flag, false, sizeof(bool)*cnt);
		best = 0;
		solve(cnt, 0);
		if(best == 0) cout<<"error"<<endl;
		else if(best==-1) cout<<"rejected"<<endl;
		else
		{
			cout<<best<<" ";
			int i = 0;
			for(; i<cnt; i++)
			{
				if(ans[i]<0)
					break;
				cout<<ans[i];
				if(i+1<cnt && ans[i+1]>=0)
					cout<<" ";
			}/**/
			cout<<endl;
		}
		delete a;
		delete flag;
		delete ans;
	}
}

void solve(int n,int idx)
{//int kk=0;for(;kk<n;kk++)if(flag[kk])cout<<a[kk]<<" ";else cout<<a[kk]<<",";
	int j=0;
	int m=0;
	int tsum=0;
	int sum = 0;
	int *tmp = new int[n];
	int cnt=0;
	for(; j<n; j++)
	{
		if(flag[j] == false)
		{
			tmp[cnt++] = a[j]+m;
			tsum += a[j]+m;
			m=0;
		}
		else
		{
			m += a[j];
			m *=10;
		}
		if(tsum > max_)
		{
			break;
		}
	}
	if(m>0)
	{
		m /= 10;
		tmp[cnt] = m;
	}
	sum = tsum + m;
	//cout<<"="<<sum<<endl;
	if(j==n && sum>best && sum<=max_ && best>=0)
	{
		memcpy(ans,tmp,sizeof(int)*n);
		best = sum;
	}
	else if(sum==best && best>0)
	{
		best = -1;
		return;
	}
	int i = idx;
	for(; i<n-1; i++)
	{
		if(flag[i] == false)
		{
			flag[i] = true;
			solve(n,i+1);
			flag[i] = false;
		}
	}
}

int main()
{
	input();
	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