Move the match stick to determine if the equation is valid

Source: Internet
Author: User

1: Read a formula from the keyboard (the formula is definitely a non-tenable equation).
2: Judge whether a stick can be moved to make the equation, then output a new equation, otherwise output No.

A The number in the formula can only be positive, the operation symbol will only appear plus and minus signs, and there is only one equal sign, no parentheses, multiplication sign or division sign, no + +,--, + + or-+ appear.
b The equation can only be integers and less than 10
C You can't move the stick, so the plus, minus, and equal signs don't change. Before and after the move, the number of sticks must be exactly the same as that shown in the computer.
D The number of the formulas that are read from the keyboard does not start with 0, but allows the numbers in the modified equation to begin with a number.

#include <stdio.h>/*define the action symbol, "+" or "-"*/  #defineSymbol_error 0#defineSymbol_add 1#defineSymbol_minus 2/*Define the change identification, the principle of value: 3 random take out n sum cannot be equal to one of the number*/  #defineFlag_no 0/* No move */#defineFlag_minus 10/* Go to a match */#defineFlag_add 100/* Add a match */#defineFlag_self 1000/* Take one of the other places on your body * */*Define legal moves*/  #defineMove_to_another 110/* 10 + 100 + 0, one number take one, put it on another number */#defineMove_to_self 1000/* 1000 + 0 + 0, take one of your own. Other positions on your body */#defineMove_from_plus 100/* 100 + 0 + 0, take a number from the plus sign on your body * */*define a match moving structure body*/typedefstructMatchstick {intIflag;/*Change Identification*/      intIData;/*matches indicate the number of*/}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 numbers that are obtained by moving a match*/  intGetdataaftermovestick (intIData, match_stick_s *Pststick) {      intICount =0; Switch(iData) { Case 3: {pststick[0].iflag = Flag_no; pststick[0].idata =3;/*Don't move*/pststick[1].iflag = Flag_add; pststick[1].idata =9;/*Add a*/pststick[2].iflag = flag_self; pststick[2].idata =2;/*move one on your own*/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;/*Don't 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 on your own*/pststick[3].iflag = flag_self; pststick[3].idata =6; pststick[4].iflag = flag_self; pststick[4].idata =9; ICount=6;  Break; }          default:          {               Break; }      }            returnICount; }    /*move match return value: Symbol_error: Illegal move Symbol_minus: from "+" to take a root added to a number symbol_add: Do not take matches from "+", move a*/  CharCheckstickmove (intIFLAG1,intIFlag2,intiFlag3) {      CharCsymbol =Symbol_error; intIflagsum = IFlag1 + IFlag2 +IFlag3; if((Move_to_another = = iflagsum) | | (Move_to_self = =iflagsum)) {Csymbol=Symbol_add; }      Else if(Move_from_plus = =iflagsum) {Csymbol=Symbol_minus; }            returnCsymbol; }    /*satisfies the move rule, and can make the equation set up, print out*/  voidPrintresult (match_stick_s *pststick1, match_stick_s *pststick2, match_stick_s *PstStick3) {      intIFLAG1 = pststick1->Iflag; intIFlag2 = pststick2->Iflag; intIFLAG3 = pststick3->Iflag; intIData1 = pststick1->IData; intIDATA2 = pststick2->IData; intIDATA3 = pststick3->IData; CharCsymbol =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, IData2, iData3); }            return; }    /*Handling Match Movement*/  voidDealstickmove (intIData1,intIDATA2,intiData3) {unsignedLongUlLoop1, ULLOOP2, ULLOOP3; intiCount1, ICount2, ICount3; match_stick_s aststick1[Ten]; match_stick_s aststick2[Ten]; match_stick_s aststick3[Ten]; 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], &aststick2[ulloop2], &ASTSTICK3[ULLOOP3]); }          }      }        return; }    intMain () {Dealstickmove (3,6,3); return 0; }  

Move the match stick to determine if the equation is valid

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.