POJ 1950

49 篇文章 0 订阅
21 篇文章 0 订阅

Dessert
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5473 Accepted: 2048

Description

FJ has a new rule about the cows lining up for dinner. Not only must the N (3 <= N <= 15) cows line up for dinner in order, but they must place a napkin between each pair of cows with a "+", "-", or "." on it. In order to earn their dessert, the cow numbers and the napkins must form a numerical expression that evaluates to 0. The napkin with a "." enables the cows to build bigger numbers. Consider this equation for seven cows: 
      1 - 2 . 3 - 4 . 5 + 6 . 7

This means 1-23-45+67, which evaluates to 0. You job is to assist the cows in getting dessert. (Note: "... 10 . 11 ...") will use the number 1011 in its calculation.) 

Input

One line with a single integer, N 

Output

One line of output for each of the first 20 possible expressions -- then a line with a single integer that is the total number of possible answers. Each expression line has the general format of number, space, napkin, space, number, space, napkin, etc. etc. The output order is lexicographic, with "+" coming before "-" coming before ".". If fewer than 20 expressions can be formed, print all of the expressions. 

Sample Input

7

Sample Output

1 + 2 - 3 + 4 - 5 - 6 + 7
1 + 2 - 3 - 4 + 5 + 6 - 7
1 - 2 + 3 + 4 - 5 + 6 - 7
1 - 2 - 3 - 4 - 5 + 6 + 7
1 - 2 . 3 + 4 + 5 + 6 + 7
1 - 2 . 3 - 4 . 5 + 6 . 7
6

Source

题目大意:一共有1,2,3,...N这样N个数字,每个相邻数字之间可以填入3个运算符:“+”,“-”,“。”  求计算结果为0的运算方案数并输出它。“。”表示连接A,B两个数字,若11,12那么"."后为1112;

我用JAVA写了一个

import java.util.Scanner;
public class poj1950 {
	private static int n=0;
	private static int number=0;
	private static int []a=new int[30];
	public static void main(String[] args) {
		
		Scanner cin=new Scanner(System.in);
		
		while(cin.hasNext()){
			
			Init();
			n=cin.nextInt();
			dfs(1,2,1);
			System.out.println(number);
		}
	}
	private static void Init(){//初始化
		number=0;
		n=0;
	}
	private static void dfs(int ans,int length,int pre){//搜答案,长度,前一个数,一起搜
		
		if(length==n+1){
			if(ans==0){
				number++;
				if(number<=20){
					System.out.print("1 ");
					for(int i=2;i<n;i++){
						if(a[i]==1){
							System.out.print("+"+" "+i+" ");
						}else if(a[i]==0){
							System.out.print("."+" "+i+" ");
						}else if(a[i]==-1){
							System.out.print("-"+" "+i+" ");
						}
					}
					if(a[n]==1){
						System.out.println("+"+" "+n);
					}else if(a[n]==0){
						System.out.println("."+" "+n);
					}else if(a[n]==-1){
						System.out.println("-"+" "+n);
					}
				}else {
					return;
				}
				
			}
			return;
			
		}
		int tem1,tem2;
		a[length]=1;
		dfs(ans+length, length+1, length);
		a[length]=-1;
		dfs(ans-length, length+1, -length);
		a[length]=0;
		if(pre>0){
			tem2=length;
		}else {
			tem2=-length;
		}
		if(length>9){
			tem1=pre*100+tem2;
		}else{
			tem1=pre*10+tem2;
		}
		dfs(ans+tem1-pre, length+1, tem1);
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值