public static void main(String[] args) {
Do(3, 20);
}
/*
* n,N项式
* m,M以内的加减法
*/
public static void Do(int n, int m)
{
int num = 2*n-1; // 随机数与符号都是随机生成
HashMap<String, String> hs = new HashMap<String, String>();
while(true) {
int[] arr=new int[num];
for (int i=0; i<num; i++){
if (i%2==0){
arr[i] = (int)(Math.random()*20);
}else{
arr[i] = (int)(Math.random()*20)%2;
}
}
int result = arr[0]; // 存放每两项相减的结果
boolean bOk = true;
for (int i=1; i<n; i++)
{
if (arr[i*2-1] == 0)
result += arr[i*2];
else
result -= arr[i*2];
if (result <0 || result > m)
{
bOk = false;
break;
}
}
if (bOk)
{
StringBuffer sb = new StringBuffer();
for (int i=0; i<num; i++)
{
if (i%2==0)
{
sb.append(arr[i]);
}else{
if (arr[i]%2 == 0)
sb.append("+");
else
sb.append("-");
}
}
if (!hs.containsKey(sb.toString()))
{
hs.put(sb.toString(), "");
System.out.println(sb.toString()+"=" + result);
}
if (hs.size() == 100 ) break;
}
}
}