C language programming of 100 cases (16): Solving equation

Example 16 Solving equation

Problem Description

Plus the number of intermediate 1,2,3,4,5,6,7,8,9,10 plus or minus sign, so that expression is obtained by a natural number N, if the symbol is not an intermediate, front and back is considered is a number, such as 123 believe one hundred twenty-three (123).

For example: When N = 100, the value of the expression fill method 100 of 24 species. 4 + 5 + 123 + 100 = 67-89-10 fill method is a 1 + 2 + 3 + 4 + 56 + 7 + 8 + 9 + 10 = 100 is also a fill method.

Write a program to find methods to fill in the value of the expression is equal to N, how many?

Input Format

Test input comprising a plurality of sets of data. Each test a natural number n, occupies a separate line. 0 indicates the end of input.

Output Format

Several test data output line, to fill out even if the value of the expression of each group is equal to n.

SAMPLE INPUT

1

10

100

0

Sample Output

45

26

24

        (1) programming ideas.

        To the left side of the equation represents the equation can define an array int a [20], where the elements a [0], a [2], ..., a [16] were stored digital 1,2, ..., 9, a [18 ] and a [19] together to save the number 10. a [1], a [3], ..., a [17] may be saved 0,1,2 fill operators, where 0 represents a space, + represents plus, minus 2 represents -. As follows:

1

 

2

 

3

 

4

 

5

 

6

 

7

 

8

 

9

 

1

0

        In this way, with a heavy cycle of three 9 9 operator may fill vacancies exhaustive, the left side of the equation to obtain the equation stored in the array a [20], and then analyzing this formula, if the calculation result is N, is a kind of the solution.

       Expression parsing program will be written as a function prototype int Parse (int a []);.

        When parsing, the array is scanned from left to right. Initially, the expression value of value = 0, the calculation should be operator preCalc = 1 (so that when the first scan to a plus or minus operator, (0) value plus the value before the operator is the operator of a number of days before, so that the loop processing can be performed), the current number of operations involved in the initial preVal to a [0].

        For a [1], a [3], ..., a [17] These nine cases treated with a fill position cycle, two cases:

         1) Fill the space in front of the number (stored in the preVal) and constituting a back of a digital number, such as a space between 1 and 2, 1 and 2 constitute the 12, if "2" is a back spaces, the "12" and "3" 123 configuration. Processing method preVal = preVal * 10 + a [i + 1];

        2) Fill is a plus or minus sign. The foregoing operation is performed (stored in the operator's operation should be carried out preCalc, attention is not current addition or subtraction, the plus or minus sign of the current operation needs to wait until the next scan plus or minus operator performed) . If preCale == 1, addition is performed, if preCale == 2, subtraction is performed. Thereafter, the operator to save the current preCale and save a number after the number of operators involved in operations in preVal.

if (preCalc==1)

         value=value+preVal;

else

   value=value-preVal;

preCalc=a[i];

preVal=a[i+1];

        (2) source.

#include <stdio.h>

int Parse(int a[]);

int main ()

{

         int a[20]={1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,1,0};

        // array a a [1], a [3], ..., a [17] may be saved 0,1,2 operator

         // where 0 represents a space, + represents plus, minus 2 represents -

         int n;

         while (scanf("%d",&n) && n!=0)

        {

               int a1, a2, a3, a4, a5, a6, a7, a8, a9, num = 0;

              for(a1=0;a1<3;a1++)

               for(a2=0;a2<3;a2++)

                 for (a3 = 0; a3 <3; a3 ++)

                   for(a4=0;a4<3;a4++)

                     for (a5 = 0; a5 <3; a5 ++)

                      for (a6 = 0; a6 <3; a6 ++)

                       for(a7=0;a7<3;a7++)

                        for(a8=0;a8<3;a8++)

                              for (a9 = 0; a9 <3; a9 ++)

                              {

                                     a[1]=a1;            a[3]=a2;            a[5]=a3;

                                     and [7] = a4. and [9] = A5; and [11] = A6;

                                     a[13]=a7;          a[15]=a8;          a[17]=a9;

                                     if (Parse(a)==n)

                                     {

                                        a ++;

                                     }

                              }

               printf("%d\n",num);

     }

     return 0;

}

int Parse(int a[])

{

         int value=0,i,preVal,preCalc;

         Preval = a [0]; preCalc = 1;

         for(i=1;i<=17;i=i+2)

         {

                   switch (a[i])

                   {

                   case 0: preVal=preVal*10+a[i+1];

                                break;

                   case 1:

                   case 2: if (preCalc==1)

                                        value=value+preVal;

                                else

                                        value=value-preVal;

                               preCalc=a[i];

                               preVal=a[i+1];

                   }

         }

         preVal=preVal*10+a[19];

        if (preCalc==1)

                  value=value+preVal;

       else

                   value=value-preVal;

    return value;

}

Exercise 16

16-1 zero number of columns Zero Sum

        This question is selected from the Los Valley exam (https://www.luogu.org/problem/P1473)

Title Description

Consider a number of 1 to N (N = 3, 4, 5 ... 9) incrementing the number of columns consisting of: 1 2 3 ... N. Now insert "+" denotes the addition in the number of columns, or "-" represents a reduction, "" represents a blank (e.g. 1-23 equals 1-23), each pair of numbers to be combined together (in the first, do not caret before digits). The results of the expression and determine if its value is 0. You are to write a program to find the length of all the produce and zero is the number of columns N.

Input Format

Single line represents an integer N (3 <= N <= 9). 

Output Format

In the order of ASCII code, is inserted between each output of all digital "+", "-", or "" to get the number of columns of the result is zero.

SAMPLE INPUT

7

Sample Output

1+2-3+4-5-6+7

1+2-3-4+5+6-7

1-2 3+4+5+6+7

1-2 3-4 5+6 7

1-2+3+4-5+6-7

1-2-3-4-5+6+7

        (1) programming ideas.

        Understand the programming ideas Example 16, an appropriate change can be solved according to the present exercise.

        The same definition array int a [17] = {1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9};

         Array a, a [1], a [3], ..., a [15] may be saved 0,1,2 operator. Wherein 0 represents a space, to represent the plus + 1, the representative minus 2 -.

        The key is how to solve this problem to a [1], a [3], ... a [2 * n-3] which the n-1 element assigned to 0,1,2 three full permutation operator configuration.

        To n = 3, for example, two kinds of operators to be assigned in a [1] and a [3] at. Full arranged as: 00,01,02,10,11,12,20,21,22 total of 9.

        Write the following recursive function may be generated either take full array of n-1 Numbers in 0,1,2 (repeatable take a number), and assigned to the corresponding a [1], a [3], ... a [2 * n-3] this n-1 elements.

void dfs(int a[], int pos,int n)

{

     if (pos == 2 * n-1) // operator has been assigned a position in the N-1

     {

          // the expression generated by analyzing the results to see whether it is 0, if it is 0, the output of the expression

          return;

     }

     for(int i = 0;i <= 2;i++)

     {

           a[pos] = i;

           dfs (a, pos + 2, n); // operator assigned to a next position

     }

}

        After generating an expression, which is also analytically thinking as in Example 16, except that to resolve this question in a [2 * n-2] so far.

       (2) source.

#include <stdio.h>

int Parse(int a[],int n);

void dfs(int a[], int pos,int n);

int main ()

{

         int a[17]={1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9};

         // array a a [1], a [3], ..., a [15] may be saved 0,1,2 operator

         // where 0 represents a space, + represents plus, minus 2 represents -

        int n;

        scanf("%d",&n);

        dfs(a,1,n);

        return 0;

}

void dfs(int a[], int pos,int n)

{

     if (pos == 2 * n-1) // operator has been assigned a position in the N-1

     {

                   if (Parse(a,n)==0)

                   {

                            char b[3]={' ','+','-'};

                            for(int i=0;i<2*n-2;i++)

                                     if (i%2==0) printf("%d",a[i]);

                                     else  printf("%c",b[a[i]]);

                            printf("%d\n",a[2*n-2]);

                   }

                  return;

     }

     for(int i = 0;i <= 2;i++)

     {

           a[pos] = i;

           dfs(a,pos+2,n);

     }

}

int Parse(int a[],int n)

{

         int value=0,i,preVal,preCalc;

         Preval = a [0]; preCalc = 1;

         for(i=1;i<2*n-1;i=i+2)

         {

                   switch (a[i])

                   {

                   case 0: preVal=preVal*10+a[i+1];

                                break;

                   case 1:

                   case 2: if (preCalc==1)

                                       value=value+preVal;

                                else

                                        value=value-preVal;

                               preCalc=a[i];

                               preVal=a[i+1];

                   }

         }

         if (preCalc==1)

             value=value+preVal;

         else

                   value=value-preVal;

         return value;

}

16-2 fill in the numbers game

Problem Description

With Equation ABCD * E = number DCBA, A, B, C, D, E represented by different programming request A, B, C, D, E.

SAMPLE INPUT

No Input

Sample Output

2178*4=8712

        (1) programming ideas 1

        Directly a (1 <= a <= 4), b (0 <= b <= 9), c (0 <= c <= 9), d (1 <= d <= 9), e (2 < = e <= 9) of various values ​​of the five digits to be exhaustive, ABCD represents a digit (i.e., s = 1000 * a + 100 * b + 10 * c + d) with a s, k represents a four-digit the DCBA (i.e., k = a + 10 * b + 100 * c + 1000 * d), if yes s * e == k and a, b, c, d, e are different from each other (i.e., a! = b && a! = c && a! = d && a! = e && b! = c && b! = d && b! = e && c! = d && c! = e && d! = e), solution of the problem is found.

        (2) a source.

#include <stdio.h>

int main ()

{

   int a,b,c,d,e,s,k;

   for(a=1;a<=4;a++)

     for (b=0;b<=9;b++)

       for(c=0;c<=9;c++)

         for(d=1;d<=9;d++)

           for (e=2;e<=9;e++)

          {

              s=1000*a+100*b+10*c+d;

              k=a+10*b+100*c+1000*d;

              if(s*e==k && a!=b && a!=c && a!=d  && a!=e

&& b!=c && b!=d && b!=e && c!=d && c!=e && d!=e)

              {

                             printf("%d*%d=%d\n",s,e,k);

               }

         }

         return 0;

}

        (3) programming ideas 2

        Since ABCD is a four-digit equation, between the most significant bit A 1 ~ 4, and each of the digits equal to each other, thus satisfying the condition can be found exhaustive four digits 1023 and 4987.

        To determine the four-digit number ABCD and E of the digits are equal to each other, a write function int isRepeat (int n, int e), if the digits of the integer n and the integers e are equal to each other, the function returns the value 1, or return 0. Analyzing method: defining a one-dimensional array b [10], for storing the respective numbers 0 to 9 times occur, the digits of the integer n decomposed, decompose each of a number i, the corresponding array element b [i] plus 1, the corresponding array element number e b [e] is also incremented by 1, and then the statistical value of the number of elements in the array b 1 cnt, if cnt is equal to 1 plus the integer n bits, each number appears once, i.e., individual numbers equal to each other, the function returns 1.

        Since the equation is the number of four digits DCBA ABCD reverse order, so write a function int reversenum (int n) in reverse order to find the number of the integer n.

        (4) 2 source.

#include <stdio.h>

int isRepeat(int n,int e);

int reversenum(int n);

int main ()

{

    int i,j,k;

    for(i=1023;i<=4987;i++)

     {

            for (j=2;j<=9;j++)

            {

                       if (isRepeat(i,j)==1) continue;

                       k=reversenum(i);

                      if (i*j==k)

                            printf("%d*%d=%d\n",i,j,k);

             }

      }

     return 0;

}

int isRepeat(int n,int e)

{

    int m=0,b[10]={0},i,cnt;

    while (n!=0)

    {

              i=n%10;  b[i]++;

              n=n/10;  m++;

    }

    b[e]++;

    cnt=0;

    for(i=0;i<=9;i++)

        if(b[i]!=0)    cnt++;

    if(cnt!=m+1)  return 1;

    else        return 0;

}

int reversenum(int n)

{

         int m=0;

         while(n!=0)

         {

                   m=m*10+n%10;

                   n=n/10;

         }

    return m;

}

        Programs written by 2 programming ideas, programming ideas than 1 much more flexible. The equation is provided to the subject ABCDE * F = EDCBA, numbers A, B, C, D, E, F representative of the different programming find A, B, C, D, E, F. Just exhaustive of the scope of the program can be changed.

        The program statement "for (i = 1023; i <= 4987; i ++)" rewritten "for (i = 10234; i <= 49876; i ++)", re-compile and execute the above procedure, obtained as shown below result.

        21978*4=87912

16-3 sloppy formula

Problem Description

Xiao Ming is impatient, often in grade school teacher wrote on the blackboard topics copied wrong. On one occasion, the teacher is entitled: 36 x 495 = copy he gave became:?? 396 x 45 = but the result is very dramatic, his answer turned out to be right! Because 36 = 396 * 495 * 45 = 17820

Such coincidences may be similar, there are many cases, such as: 594 * 27 * 54 = 297.

Suppose a, b, c, d, e represents 1 to 9, five different numbers (note number is not the same, and no 0), to meet the form: ab * cde = adb * ce such total formula how many do?

Input Format

A positive integer n (5≤n≤9), represent different a, b, c, d, e representative of numbers 1 ~ n 5.

Output Format

An integer that represents a number satisfying the form ab * cde = expression of several such adb * ce.

SAMPLE INPUT

9

Sample Output

142

        (1) 1 programming ideas.

        This is the key problem to be exhaustive a, b, c, d, e take full array 5 consisting of different numbers between 1 ~ n.

        int num [6], wherein the num [1] ~ num [5] respectively represent five elements a, b, c, d, e, define an array value. Define an array int visit [10] = {0} numerals appears, such as visit [3] = 1 represents the number 3 has already been used, visit [3] = 0 represents the number 3 has not been used. Write the following recursive function 1 ~ n may be generated in either take full array composed of five digits.

void DFS (int num [], int pos, int n, int Visit []) 

 { 

     if (pos == 6) // number has 5 

     { 

             for (int i=1;i<6;i++)

                   printf ( "% d", whether [i]);

             printf(“\n”);

             return; 

     } 

     For (int i = 0 ;i <= n;i++) 

     { 

        if(!visit[i]) 

        { 

           num[pos] = i; 

           visit[i] = 1; 

           dfs(num,pos+1,n,visit); 

           visit[i] = 0; 

        } 

     } 

        Generating different 1 to n 5 of digits is stored into the array, corresponding to detection of expression ab * cde == adb * ce is established or if established, is a set of solutions, and the output count.

        (2) a source.

#include <stdio.h>

int cnt=0;

void DFS (int num [], int pos, int n, int Visit [])

 {

     if (pos == 6) // number has 5

     {

         From int num [1] * 10 + num [2];

         cd int num [3] * 100 + num [4] * 10 + num [5];

         ADB int num [1] * 100 + num [4] * 10 + num [2];

         ce int num [3] * 10 + num [5];

         if(ab*cde == adb*ce)

          {

                 cnt++;

         }

         return;

     }

     for(int i = 1;i <= n;i++)

     {

        if(!visit[i])

        {

           num[pos] = i;  visit[i] = 1;

           dfs(num,pos+1,n,visit);

           visit[i] =0;

        }

     }

}

int main ()

{

         int num [6], n;

         int visit[10];

         scanf("%d",&n);

         for (int i=1;i<=9;i++)

                   visit[i]=0;

         DFS (whether 1, n, visit);

         printf("%d\n",cnt);

    return 0;

}

 

        Of course, this question without recursion, using an exhaustive cycle can be easily resolved. Prepared by the procedure shown below source cycle 2 to be exhaustive.

       (3) the source 2.

#include <stdio.h>

int main ()

{

    int n;

    scanf("%d",&n);

    int a,b,c,d,e,cnt=0;

    for(a = 1; a <= n;a++)

     for(b = 1; b <= n;b++)

       for(c = 1; c <= n;c++)

         for(d = 1; d <= n;d++)

           for(e = 1; e <=n;e++)

           {

                 int ab = 10*a + b;

                 int cde = 100*c + 10*d + e;

                 int adb = 100*a + 10*d + b;

                 the int * c + e = 10;

                 if(ab*cde != adb*ce)

                     continue;

                 int num [10] i;

                 for (i=1;i<=9;i++)

                      whether [i] = 0;

                 whether [the] ++; Surely [b] ++; whether [a] ++;

                 Surely [d] ++; Surely [e] ++;

                 for(i = 1;i <= 9;i++)

                      if(num[i] > 1)  break;

                 if(i == 10)

                 {

                     cnt++;

                  }

           }

         printf("%d\n",cnt);

    return 0;

}

Guess you like

Origin www.cnblogs.com/cs-whut/p/11919388.html