Recently have a friend to find a job, interview a company, met a match stick moving face test, feel a bit of meaning, here to stimulate.
The topic is broadly like this:
A three-digit formula that moves one of the matches, making the equation set up, using a program or implementation (with pseudo-code ) to output an equation that can be set.
Note: "+" can remove a match and turn it into a "-"
For example:
Nonsense not to say, directly on the code, which netizens have a better way, please share, thank you
#include <stdio.h>/* define the action symbol, "+" or "-" */#define SYMBOL_ERROR 0#define symbol_add 1#define symbol_minus 2/* Define the change identifier, take the value Principle: 3 random take out n sum cannot be equal to one of the number */#define FLAG_NO 0/* Not moved */#define FLAG_MINUS 10/* Go to a match */#define FLAG_ADD 100 /* Add a match */#define FLAG_SELF 1000/* Take one and put it in the other position *//* define legal movement */#define MOVE_TO_ANOTHER 110/* 10 + 100 + 0, one count, put To another number on the */#define MOVE_TO_SELF 1000/* 1000 + 0 + 0, take one of your own on the other position */#define MOVE_FROM_PLUS 100/* 100 + 0 + 0, take from the plus sign Put a number on the body *//* define the match moving structure */typedef struct matchstick{int iflag;/* Change logo */int iData;/* matches indicated number */}match_stick_s;/* Gets the number of a number that does not move or moves a match (only lists 3 and 6, other values are similar) enter parameter: Specified number (0-9) Travel: match_stick_s structure Array return value: The number of moving a match gets */int Getdataaftermovestick ( int IData, match_stick_s *pststick) {int iCount = 0; Switch (iData) {case 3: {pststick[0].iflag = flag_no; pststick[0].idata = 3; /* Do not move */Pststick[1].iflag = Flag_add; Pststick[1].idata = 9; /* Add a */PSTSTIck[2].iflag = flag_self; Pststick[2].idata = 2; /* Move one */Pststick[3].iflag = flag_self; Pststick[3].idata = 3; Pststick[4].iflag = flag_self; Pststick[4].idata = 5; ICount = 5; Break } Case 6: {pststick[0].iflag = flag_no; pststick[0].idata = 6; /* Do not move */Pststick[1].iflag = Flag_add; Pststick[1].idata = 8; /* Add a */Pststick[5].iflag = Flag_minus; Pststick[5].idata = 5; /* Minus one */Pststick[2].iflag = flag_self; Pststick[2].idata = 0; /* Move one */Pststick[3].iflag = flag_self; Pststick[3].idata = 6; Pststick[4].iflag = flag_self; Pststick[4].idata = 9; ICount = 6; Break } default: {break; }} return iCount;} /* Move match return value: Symbol_error: Illegal move Symbol_minus: Take a root from "+" to a number symbol_add: When you do not take a match from "+", move a */char checkstickmove (int i FLAG1, int iFlag2, int iFlag3) {char csymbol = symbol_error; int iflagsum = IFlag1 + IFlag2 + iFlag3; if ((Move_to_another = = iflagsum) | | (move_to_self = = iflagsum)) {csymbol = Symbol_add; } else if (Move_from_plus = = iflagsum) {csymbol = Symbol_minus; } return Csymbol;} /* Satisfy the move rule, and can make the equation set up, print out */void printresult (match_stick_s *pststick1, match_stick_s *pststick2, match_stick_s * PSTSTICK3) {int IFLAG1 = pststick1->iflag; int iFlag2 = pststick2->iflag; int IFLAG3 = pststick3->iflag; int iData1 = pststick1->idata; int iData2 = pststick2->idata; int iData3 = pststick3->idata; Char Csymbol = Checkstickmove (IFlag1, IFlag2, IFLAG3); if ((Symbol_add = = Csymbol) && (iData3 = = iData1 + iData2)) {printf ("%d +%d =%d\n", IData1, IData2, iData3 ); } else if ((Symbol_minus = = Csymbol) && (iData3 = = idata1-idata2)) {printf ("%d-%d =%d\n", IData1, ID ATA2, IDATA3); } return; /* Handle match Move */void Dealstickmove (int iData1, int iData2, int iData3) {unsigned long ulLoop1, ULLOOP2, ULLOOP3; int ICount1, ICount2, ICount3; match_stick_s ASTSTICK1[10]; match_stick_s ASTSTICK2[10]; match_stick_s ASTSTICK3[10]; ICount1 = Getdataaftermovestick (iData1, ASTSTICK1); ICount2 = Getdataaftermovestick (IData2, AstStick2); ICount3 = Getdataaftermovestick (iData3, ASTSTICK3); for (ULLOOP1 = 0, ulLoop1 < iCount1; ulloop1++) {for (ulLoop2 = 0; ulLoop2 < ICount2; ulloop2++) { for (ulLoop3 = 0; ulLoop3 < ICount3; ulloop3++) {Printresult (&ASTSTICK1[ULLOOP1 ], &ASTSTICK2[ULLOOP2], &aststick3[ulloop3]); }}} return;} int main () {Dealstickmove (3, 6, 3); return 0;}
Operation Result:
3 + 0 = 3
9-6 = 3