Match moving face question

Source: Internet
Author: User

Recently, a friend of mine found a job, interviewed the company, we met a match stick mobile interview questions, I feel a bit of meaning, here to stimulate.


The topic is roughly this:

A three-digit formula that moves a match. Make the equation set up. Using a program or implementation (capable of using pseudo-code ) to output an equation that can be established.

Note: "+" can remove a match and turn it into a "-"

Like what:



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: The 3 randomly removed n sum cannot be equal to one of the number */#define FLAG_NO 0/* Not moved */#define 10/* to a match */#define FLAG_ADD 100 /* Add a match */#define FLAG_SELF 1000/* Take one and put it in the other position *//* define the legal move */#define MOVE_TO_ANOTHER 110/* 10 + 100 + 0, one count, put There is a number on the */#define MOVE_TO_SELF 1000/* 1000 + 0 + 0, take one of your own body other locations */#define MOVE_FROM_PLUS 100/* 100 + 0 + 0, 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 is not moving or moving a match (just lists 3 and 6, other values are similar) ENTER: Specified number (0-9) Travel: match_stick_s structure Array return value: The number of */int to move a match 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 (&AMP;ASTSTICK1[ULLOOP1            ], &AMP;ASTSTICK2[ULLOOP2], &aststick3[ulloop3]); }}} return;}        int main () {Dealstickmove (3, 6, 3); return 0;}


Execution Result:

3 + 0 = 3
9-6 = 3

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Match moving face question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.