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 suhang at 2010-01-04 20:40:07 on Problem 1950
#include <cstdio>

const int maxn=20;
int n,ans=0;
char a[3]={'+','-','.'};
char c[maxn];
int count[maxn];
char b[maxn];

void dfs(int u)
{
	if (u==n)
	{
		int l=1,y=0;
		count[l]=1;
		for (int i=1;i<n;i++)
		{
		if (c[i]!=a[2])
		{
			l++;	
			count[l]=i+1;
			b[l-1]=c[i];
		}
		else
		{
			if (i<9)
			count[l]=count[l]*10+i+1;
			else
			count[l]=count[l]*100+i+1;
		}
		}
		y=count[1];
		for (int i=1;i<=l-1;i++)
		{
			if (b[i]==a[0])
			{
				y+=count[i+1];
			}
			else
			{
				y-=count[i+1];
			}
		}
		if (y==0)
		{
		ans++;
		if (ans<=20)
		{
			for (int i=1;i<=n-1;i++)
			printf("%d %c ",i,c[i]);
			printf("%d\n",n);
		}
		}
		return;
	}
	for (int i=0;i<=2;i++)
	{
		c[u]=a[i];
		dfs(u+1);
	}
}

int main()
{
	freopen("p1950.in","r",stdin);
	freopen("p1950.out","w",stdout);
	
	scanf("%d",&n);
	c[0]='+';
	dfs(1);
	printf("%d\n",ans);
}

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