C + + Programming fun question 1 in the middle of the 1~9 to fill the result equals 100

Source: Internet
Author: User

In 1 2 3 4 5 6 7 8 99 digits Insert a "+" or "-" symbol to make the result 100, which is programmed to implement all combinations. Note: The order of numbers cannot be changed

<pre name= "code" class= "CPP" >nclude <stdio.h> #include <iostream> #include <memory.h> #define NUM 6561//3^8:6561using namespace Std;int mici (int x, int i)//solve x^i{int res = 1; for (; I >0; i--) {res *= x;} return (res);} int main (int argc, char *argv[]) {int data[9]; int index, level; int I, J, k=0; int pre_sign; int temp = 0; int sum = 0; I NT x = 2;   for (i=0; i<num; i++)//total of 6561 possible {memset (data, 0, sizeof (int) *9);  Clear 0 index = i;              for (level=9; level>1; level--)//There are 8 symbols to be judged {pre_sign = index%3;                 The empty symbol, which specifies 0:+,1:-;2: Connect index = INDEX/3;       Position of the previous layer (that is, the previous empty) switch (pre_sign) {case 0:if (temp = = 0) {Data[k] = level;     } else {data[k] = temp;     temp = 0;    x = 2;    } k++;   Break       Case 1:if (temp = = 0) {Data[k] = 0-level;     } else {data[k] = 0-temp;     temp = 0;    x = 2; } K ++;   Break    Case 2:if (temp = = 0) {temp = level + (LEVEL-1) *10;     } else {temp = temp + (LEVEL-1) *mici (10,x);    x + +;   } break;   Default:break;  }} if (pre_sign = = 2)//processing number 1 {data[k] = temp;  } else {data[k] = 1;                          } k = 0;  It is important to reset these variables to avoid affecting the next round!  temp = 0;  x = 2;  for (j=0; data[j]!=0; j + +)//sum {sum = sum + data[j]; if (sum = =) {for (j=j-1; j>=0; j--)//reverse output, so 1 in front {if (Data[j] >0) {cout<&lt ;"    + "<<data[j];    } else {cout<<data[j]; }} cout<< "=" <<sum<</* "..." <<i<<*/endl; I for test} sum = 0; } return (0);}


</pre><pre name= "code" class= "CPP" >
  This is I accidentally see on the net, feel very interesting, oneself on the computer to try a bit, more than 100 lines of source code dozen more than half an hour, finally also appeared the mistake, finally in a professional seniors help solve the problem, the original compiler is caused by different. It looks like you have to get a good idea of the different problems that are caused by different compilers. There are 6561 possible comments, but I do not know why there are only 11 of the last, to strengthen the improvement AH
  Believe that you can someday write this code to continue to refuel!!! Up, Junior ^0^.

C + + Programming fun question 1 in the middle of the 1~9 to fill the result equals 100

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.