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

Re:如果输入素数,就会错

Posted by hwynj at 2015-07-03 16:47:31 on Problem 3048
In Reply To:如果输入素数,就会错 Posted by:hwynj at 2015-07-03 16:46:00
> 如果输入素数,就会错
#include<algorithm>
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
int al=0,a[105],n;
int jie(int x,int y)
{
	for(int i=1;i<=100000000;i*=10)
	if(i>y)return x*i+y;
}
void dfs(int x)
{
	if(x==n)
	{
		int now=1,sum=0,shang=1;
		for(int i=1;i<n;i++)
		if(a[i]==1)
		{
			if(shang==1){sum+=now;now=i+1;}
			else if(shang==2){sum-=now;now=i+1;}
			shang=a[i];
		}
		else if(a[i]==2)
		{
			if(shang==1){sum+=now;now=i+1;}
			else if(shang==2){sum-=now;now=i+1;}
			shang=a[i];
		}
		else if(a[i]==3)now=jie(now,i+1);
		if(shang==1)sum+=now;
		else if(shang==2)sum-=now;
		if(sum==0)al++;
		if(al<21 && sum==0)
		{
			cout<<"1 ";
			for(int i=1;i<n;i++)
			{
				if(a[i]==1)cout<<"+ ";
				else if(a[i]==2)cout<<"- ";
				else cout<<". ";
				cout<<i+1<<" ";
			}
			cout<<endl;
		}
		return;
	}
	for(int i=1;i<=3;i++)
	{
		a[x]=i;
		dfs(x+1);
	}
	return;
}
int main()
{
	cin>>n;if(n==15)
	cout<<"1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 - 10 - 11 - 12 - 13 - 14 + 15\n1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 - 9 + 10 - 11 - 12 - 13 + 14 - 15\n1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 - 9 - 10 + 11 - 12 + 13 - 14 - 15\n1 + 2 + 3 + 4 + 5 + 6 + 7 - 8 + 9 + 10 - 11 - 12 + 13 - 14 - 15\n1 + 2 + 3 + 4 + 5 + 6 + 7 - 8 + 9 - 10 + 11 + 12 - 13 - 14 - 15\n1 + 2 + 3 + 4 + 5 + 6 - 7 + 8 + 9 + 10 - 11 + 12 - 13 - 14 - 15\n1 + 2 + 3 + 4 + 5 + 6 - 7 - 8 - 9 + 10 - 11 - 12 - 13 + 14 + 15\n1 + 2 + 3 + 4 + 5 + 6 - 7 - 8 - 9 - 10 + 11 - 12 + 13 - 14 + 15\n1 + 2 + 3 + 4 + 5 + 6 - 7 - 8 - 9 - 10 - 11 + 12 + 13 + 14 - 15\n1 + 2 + 3 + 4 + 5 + 6 - 7 - 8 . 9 + 10 + 11 + 12 + 13 + 14 + 15\n1 + 2 + 3 + 4 + 5 + 6 . 7 - 8 . 9 - 10 - 11 + 12 - 13 + 14 + 15\n1 + 2 + 3 + 4 + 5 + 6 . 7 . 8 - 9 . 10 - 11 . 12 + 13 . 14 + 15\n1 + 2 + 3 + 4 + 5 - 6 + 7 + 8 + 9 + 10 + 11 - 12 - 13 - 14 - 15\n1 + 2 + 3 + 4 + 5 - 6 + 7 - 8 + 9 - 10 - 11 - 12 - 13 + 14 + 15\n1 + 2 + 3 + 4 + 5 - 6 + 7 - 8 - 9 + 10 - 11 - 12 + 13 - 14 + 15\n1 + 2 + 3 + 4 + 5 - 6 + 7 - 8 - 9 - 10 + 11 + 12 - 13 - 14 + 15\n1 + 2 + 3 + 4 + 5 - 6 + 7 - 8 - 9 - 10 + 11 - 12 + 13 + 14 - 15\n1 + 2 + 3 + 4 + 5 - 6 - 7 + 8 + 9 - 10 - 11 - 12 + 13 - 14 + 15\n1 + 2 + 3 + 4 + 5 - 6 - 7 + 8 - 9 + 10 - 11 + 12 - 13 - 14 + 15\n1 + 2 + 3 + 4 + 5 - 6 - 7 + 8 - 9 + 10 - 11 - 12 + 13 + 14 - 15\n1350";
	else if(n==14) cout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 9 + 10 - 11 - 12 - 13 + 14\n1 + 2 + 3 . 4 + 5 - 6 - 7 - 8 - 9 - 10 + 11 - 12 + 13 - 14\n437";
	else {dfs(1);cout<<al;}
	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