一个一个的穷举吧,在填充符号的时候按照‘ ’、‘+’、‘-’的顺序填充就是题目要求的所谓的“ASCII order”
Code
/**//*
ID: sdjllyh1
PROG: zerosum
LANG: JAVA
complete date: 2008/12/18
complexity: O(3^n)
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class zerosum
{
private static int n;
private static char[] operators;//填充的符号
private static ArrayList expression = new ArrayList();//符合要求的表达式
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
insertOperator(1);
}
//递归填充符号,pos是填充位置
private static void insertOperator(int pos)
{
//如果所有位置都填充完毕
if (pos == n)
{
int sum = 0;//各个数字的和,即表达式的值
int digit = 1;//当前遇到的数字
int operator = 1;//当前遇到的数字前面的符号,-1表示减号
for (int i = 1; i < n; i++)
{
switch (operators[i])
{
case ' ':
digit = digit * 10 + i + 1;
break;
case '+':
sum += digit * operator;
operator = 1;
digit = i + 1;
break;
case '-':
sum += digit * operator;
operator = -1;
digit = i + 1;
break;
default:
String execution = "kill the USACO";
break;
}
}
sum += digit * operator;
if (sum == 0)
{
expression.add(makeAnExpression());
}
}
else
{
//考虑三种情况,递归填充符号
operators[pos] = ' ';
insertOperator(pos + 1);
operators[pos] = '+';
insertOperator(pos + 1);
operators[pos] = '-';
insertOperator(pos + 1);
}
}
private static String makeAnExpression()
{
StringBuffer exp = new StringBuffer();
for (int i = 1; i < n; i++)
{
exp.append(i);
exp.append(operators[i]);
}
exp.append(n);
return exp.toString();
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("zerosum.in"));
n = Integer.parseInt(f.readLine());
f.close();
operators = new char[n];
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("zerosum.out")));
Iterator it = expression.iterator();
while (it.hasNext())
{
out.println(it.next());
}
out.close();
}
}
/**//*
ID: sdjllyh1
PROG: zerosum
LANG: JAVA
complete date: 2008/12/18
complexity: O(3^n)
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class zerosum
{
private static int n;
private static char[] operators;//填充的符号
private static ArrayList expression = new ArrayList();//符合要求的表达式
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
insertOperator(1);
}
//递归填充符号,pos是填充位置
private static void insertOperator(int pos)
{
//如果所有位置都填充完毕
if (pos == n)
{
int sum = 0;//各个数字的和,即表达式的值
int digit = 1;//当前遇到的数字
int operator = 1;//当前遇到的数字前面的符号,-1表示减号
for (int i = 1; i < n; i++)
{
switch (operators[i])
{
case ' ':
digit = digit * 10 + i + 1;
break;
case '+':
sum += digit * operator;
operator = 1;
digit = i + 1;
break;
case '-':
sum += digit * operator;
operator = -1;
digit = i + 1;
break;
default:
String execution = "kill the USACO";
break;
}
}
sum += digit * operator;
if (sum == 0)
{
expression.add(makeAnExpression());
}
}
else
{
//考虑三种情况,递归填充符号
operators[pos] = ' ';
insertOperator(pos + 1);
operators[pos] = '+';
insertOperator(pos + 1);
operators[pos] = '-';
insertOperator(pos + 1);
}
}
private static String makeAnExpression()
{
StringBuffer exp = new StringBuffer();
for (int i = 1; i < n; i++)
{
exp.append(i);
exp.append(operators[i]);
}
exp.append(n);
return exp.toString();
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("zerosum.in"));
n = Integer.parseInt(f.readLine());
f.close();
operators = new char[n];
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("zerosum.out")));
Iterator it = expression.iterator();
while (it.hasNext())
{
out.println(it.next());
}
out.close();
}
}