面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
2014-10-25 21:51
471 查看
package com.Amazon.interview; /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.cnblogs.com/lkzf/ * @Time: 2014年10月25日下午5:14:33 * ************* function description *************** * * 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" * * 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. * * 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 * * Sample Input and Output: * * Input: * * 1 2 3 4 10 * * 1 2 3 4 5 * * Output: * * 1+2+3+4=10 * * Invalid * * * **************************************************** */ public class GenerateEquation { public static void createEqualAndPrint(int[] a, int n, int target) { if (a == null || a.length == 0 || a.length != n || n == 0) { System.out.println("Invalid"); return; } if (n < 1 || n > 15) { System.out.println("Invalid"); return; } int i = n - 1; int v = 1; while (i > 1) { v = (v << 1) + 1; i--; } int sum = 0; String s = null; while (v > 0) { sum = a[0]; s = a[0] + ""; for (int j = n - 1; j > 0; j--) { int c = v >> (j - 1); if ((c & 1) == 1) { sum += a[n - j]; s += "+" + a[n - j]; } else { sum -= a[n - j]; s += "-" + a[n - j]; } } if (sum == target) { System.out.println(s + "=" + target); return; } else { v--; } } System.out.println("Invalid."); } /** * @param args */ public static void main(String[] args) { int[] a = { 1, 2, 3, 4 }; createEqualAndPrint(a, 4, 2); } }
相关文章推荐
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- GeeksforGeeks Sort an array according to the order defined by another array
- Misleading error message in PowerShell script: "Invalid assignment expression. The left hand side of an assignment operator need
- 错误 the type "xxxx" is defined in an assembly that is not refernced.You must add a reference to assem
- How to combine the value of multiple hashes within an array by the same key
- The type of the expression must be an array type but it resolved to Point
- The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List
- Alias "bootstrap.widgets.TbHeroUnit" is invalid. Make sure it points to an existing PHP file and the
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- mm_The type of the expression must be an array type but it resolved to List
- "Unable to add selected web parts. (insert part name): An error has occured."
- Could not use "Ocean configurator" to add the ocean module to the ocean module project
- HOWTO:How to make the WebGrid work like an Excel sheet
- Question 12: In C++, which of the following is the best declaration for an overloaded operator[] to allow read-only access (and
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- Programming Basic--why we need to add ' extern "C" ' in C++ program when calling the function which is complied by C complier?
- From an inner class, "this" points to the inner object. How do I get a "this" pointer to the enclosing object?
- Algorithm: Make all the odd numbers precede all the even numbers in an array.
- 当打开Oracle Jdevelopler 11g时出现"Unable to create an instace of the jave virtual Machine Located at path"错时解决办法
- Algorithm: Make all the odd numbers precede all the even numbers in an array.