There are 3 ^ 8 possibilities. Answer: Success: 12 + 34 + 56 + 7-8 + 9 = 110
Success: 12 + 3 + 45 + 67-8-9 = 110
Success: 12-3 + 4-5 + 6 + 7 + 89 = 110
Success: 1 + 2 + 34 + 5 + 67-8 + 9 = 110
Success: 1-2 + 3 + 45-6 + 78-9 = 110
Success: 123 + 4-5-6-7-8 + 9 = 110
Success: 123-4 + 5-6-7 + 8-9 = 110
Success: 123-4-5 + 6 + 7-8-9 = 110
Succeed: 123 + 4 + 5 + 67-89 = 110
Success: 1 + 234-56-78 + 9 = 110
Two methods:
/*** Copyright (c) 2012 Lear C * // *** 1 2 3 4 5 6 7 8 9 = 110 * <p/> * fill in the plus sign or minus sign between the numbers (you can leave it blank, but cannot be filled with other symbols) to make the equation true. <Br/> * a better method is: <br/> * There are three possibilities for each gap: "+ ","-","", therefore, there are 3 ^ 8 possibilities. ** @ Author Lear */public class tester2 {Private Static final char [] numbers = {'1', '2', '3', '4', '5 ', '6', '7', '8', '9'}; Private Static final string [] operators = {"+ ","-",""}; private Static final int result = 110; // calculation result public static void main (string [] ARGs) {sortandcompute (0, "");} Private Static void sortandcompute (INT numindex, string Buffer) {// specifies the last character if (numindex = numbers. lengt H-1) {buffer + = numbers [numindex]; string formula = buffer. tostring (); If (sum (formula) = Result) {system. out. println (formula + "=" + result);} return ;}for (INT operindex = 0; operindex <operators. length; ++ operindex) {buffer + = numbers [numindex]; buffer + = operators [operindex]; sortandcompute (numindex + 1, buffer ); // remove the first two added characters to restore the original state, so that the next loop is superimposed // but when the intermediate connector is changed, delete only the first character buffer = operind in the buffer. Ex! = 2? Buffer. substring (0, buffer. length ()-2): buffer. substring (0, buffer. length ()-1) ;}} Private Static int sum (string formula) {If (formula = NULL | formula. trim (). length () = 0) throw new illegalargumentexception ("formula is invalid! "); Stack <string> numstack = new stack <string> (); stack <string> operstack = new stack <string> (); stringbuffer numbuffer = new stringbuffer (); formula + = "#"; // Add an terminator to the end of the formula to facilitate the calculation of char [] CHS = formula. tochararray (); For (INT Index = 0; index <formula. length (); ++ index) {If (CHS [Index]! = '+' & CHS [Index]! = '-' & CHS [Index]! = '#') {Numbuffer. append (CHS [Index]);} else {numstack. push (numbuffer. tostring (); numbuffer. delete (0, numbuffer. length (); If (operstack. isempty () operstack. push (CHS [Index] + ""); else {int numaft = integer. parseint (numstack. pop (); int numbef = integer. parseint (numstack. pop (); string Signature = operstack. pop (); int sum = ignore. equals ("+ ")? Numbef + numaft: numbef-numaft; numstack. push (sum + ""); operstack. push (CHS [Index] + "") ;}} return integer. parseint (numstack. pop ());}}
Package test; import Java. util. arraylist; import Java. util. collections; import Java. util. list;/*** 1 2 3 4 5 6 7 8 9 = 110 * <p/> * fill in the plus sign or minus sign between numbers (you can leave it blank, but cannot enter other symbols) make the equation true. * @ Author Lear **/public class tester {Private Static final char [] numbers = {'1', '2', '3', '4', '5 ', '6', '7', '8', '9'/**/}; Private Static final int result = 110; // calculation result public static void main (string [] ARGs) {list <string> All = sort (numbers); testprint (all ); for (list <string> arank: All) {printestablishequation (arank) ;}} Private Static void testprint (list <string> All) {for (list <string> Arank: All) {system. out. println (arank) ;}}/*** sort and combine by Nums, and return a list array, it will contain all possible list arrays consisting of one column of data. * <P/> * This is a recursive function. The characters after the first combined number continue to call this function to calculate the list <string>. <br/> * disadvantage: memory overflow occurs when the data volume increases, and the algorithm efficiency is low. ** @ Param Nums * @ return format: [[, 3, 4 ..], [12, 3, 4,...], [,...],...] */Private Static list <string> sort (char [] Nums) {If (nums. length = 0) return collections. emptylist (); List <list <string> All = new arraylist <list <string> (); // Add the character array directly to the list <string> firstrank = new arraylist <string> (); f Or (INT I = 0; I <nums. length; ++ I) {firstrank. add (Nums [I] + "");} All. add (firstrank); // The number of combined numbers, for example, 1, 2, 3, 4 ....; 12, 3, 4, 5 ..; 123,4, 5 ..; 1234.5... for (INT combinationnum = 2; combinationnum <= nums. length; ++ combinationnum) {// offset of the combination, for example, 12, 3 ....; 1, 23, 4 ....; 1, 2, 34 ,... for (INT offset = 0; offset <nums. length-(combinationnum-1); ++ offset) {list <string> prerank = new arraylist <string> (); string Builder buffer = new stringbuilder (); For (INT I = 0; I <OFFSET; ++ I) {// front prerank. add (Nums [I] + "") ;}for (INT I = offset; I <OFFSET + combinationnum; ++ I) {// buffer. append (Nums [I]);} prerank. add (buffer. tostring (); // obtain the character array next to it, and recursively combine char [] suffix = new char [nums. length-(Offset + combinationnum)]; for (INT I = offset + combinationnum, Index = 0; I <nums. length; ++ I, ++ index) {// suffix [Index] = Nums [I];} // For example, after the 12 combination [[3, 4, 5, 6, 7...], [34,5, 6...], [345...] list <list <string> sufarray = sort (suffix); // Add a combination of the preceding numbers for all lists <string>. // example: add 12 to the example above to make it [[, 3, 4,...], [12, 34...] if (sufarray. size ()! = 0) for (list <string> sufrank: sufarray) {// listlist before and after the combination <string> allrank = new arraylist <string> (); For (INT I = 0; I <prerank. size (); ++ I) allrank. add (prerank. get (I); For (INT I = 0; I <sufrank. size (); ++ I) allrank. add (sufrank. get (I); // Add it to all to go to all. add (allrank);} elseall. add (prerank); // description to the end} return all;} Private Static void printestablishequation (list <string> ls) {char [] operators = {'+ ', '-'}; Stringbuilder buff = new stringbuilder (); // converts it to a numeric int [] Nums = new int [ls. size ()]; for (INT I = 0; I <ls. size (); ++ I) {Nums [I] = integer. parseint (LS. get (I);} // array of variable operators Boolean [] isoperchanges = new Boolean [nums. length-1]; // calculate the change period of each isoperchange int [] peroperchangecounts = new int [isoperchanges. length]; for (INT Index = 0; index <isoperchanges. length; ++ index) {peroperchangecounts [Index] = (I NT) math. pow (2, index);} // number of possible computations 2 ^ (nums. length-1) int computecount = (INT) math. pow (2, Nums. length-1); For (INT I = 1; I <= computecount; ++ I) {// iteration calculation int sum = Nums [0]; buff. append (Nums [0]); For (INT Index = 0; index <nums. length-1; ++ index) {sum = isoperchanges [Index]? Sum-nums [index + 1]: Sum + Nums [index + 1]; buff. append (isoperchanges [Index]? Operators [1]: operators [0]); buff. append (Nums [index + 1]);} // print if (sum = Result) // output the formula system. out. println ("success:" + buff. tostring () + "=" + sum); // else // system. out. println ("failed:" + buff. tostring () + "=" + sum); buff. delete (0, Buff. length (); // The iteration calculation of the array that changes alternately by the operator. // 1st operator, alternating every time; 2nd operator, I changed every 2 ^ 2 times; 3rd operator, I changed every 2 ^ 3 times for (INT Index = 0; index <isoperchanges. length; ++ index) {if (I % peroperchangecounts [Index] = 0) isoperchanges [Index] =! Isoperchanges [Index]; // alternate }}}}