Question:
Given an array with positive integers and another integer for example{7 2 4} 9, you are required to generate an equation, by inserting operator add ("+") and minus ("-") among the array . The left side of equation are consist of the array and the right side of equation is the integer. here the result is 7-2+4=9
Rules:
Don't include any space in the generated equation.
In case there is no way to create the equation, please output "Invalid". For example {1 1} 10, output is "Invalid"
The length of the integer array is from 1 to 15( include 1 and 15). If the length is 1, for example the input {7} 7, the output is 7=7
There is no operator "+" or "-" in front of the first number:
Don't change the order of the numbers. For example: {7 2 4} 9. 7-2+4=9 is correct answer, 4-2+7=9 is wrong answer.
There could be multiple input, meaning your function could be called multiple times. Do remember print a new line after the call.
Sample Input and Output:
Input:
1 2 3 4 10
1 2 3 4 5
Output:
1+2+3+4=10
Invalid
---------------------------
基本思路:
1.题目固定了等式左侧数值位置,即只需要向数之间插入“+”、“-”,使得等式成立。
2.用“1”、“0”代替“+”、“-”号,遇1则加值,遇0则减值。
Given an array with positive integers and another integer for example{7 2 4} 9, you are required to generate an equation, by inserting operator add ("+") and minus ("-") among the array . The left side of equation are consist of the array and the right side of equation is the integer. here the result is 7-2+4=9
Rules:
Don't include any space in the generated equation.
In case there is no way to create the equation, please output "Invalid". For example {1 1} 10, output is "Invalid"
The length of the integer array is from 1 to 15( include 1 and 15). If the length is 1, for example the input {7} 7, the output is 7=7
There is no operator "+" or "-" in front of the first number:
Don't change the order of the numbers. For example: {7 2 4} 9. 7-2+4=9 is correct answer, 4-2+7=9 is wrong answer.
There could be multiple input, meaning your function could be called multiple times. Do remember print a new line after the call.
Sample Input and Output:
Input:
1 2 3 4 10
1 2 3 4 5
Output:
1+2+3+4=10
Invalid
---------------------------
基本思路:
1.题目固定了等式左侧数值位置,即只需要向数之间插入“+”、“-”,使得等式成立。
2.用“1”、“0”代替“+”、“-”号,遇1则加值,遇0则减值。
3.构造等式即可以转化成求一个在[0,2^n)间的值的二进制数,使得arry中的数按照规则2得到final值。若不存在该数则返回“Invalid”。
package CreateEquationAndPrint;
public class CreateEquation
{
public CreateEquation(){}
public static int splitAndConvert(String string,int array[])
{
String[] tokenPtr = string.split(" ");
int i= tokenPtr.length;
return i;
}
public static void createEquationAndPrint(int[] array,int length, int result)
{
int tokenLen = (int) Math.pow(2, length-1);
boolean flag = false;
for(int i=0;i<tokenLen;i++)
{
int[] token = new int[length-1];
int count=0;
for(int j=0;j<length-1;j++)
{
if(((1<<j)&i)!=0) token[length-2-j]=1;
else token[length-2-j]=0;
count++;
}
int sum =array[0];
String equation = String.valueOf(array[0]);
for(int k=1,j=0;j<count && k<length-1;j++,k++)
{
if(token[j]==1)
{
sum += array[k];
equation+="+"+String.valueOf(array[k]);
}
else if(token[j]==0)
{
sum -= array[k];
equation+="-"+String.valueOf(array[k]);
}
}
if(sum==result)
{
flag = true;
equation+="="+result;
System.out.println(equation);
break;
}
}
if(flag==false)
System.out.println("Invalid");
}
public static void main(String[] args)
{
int[] array = new int[]{1,2,3,4,10};
int length = array.length;
createEquationAndPrint(array,length,array[length-1]);
}
}