C language programming of 100 cases (18): Equation matchstick

Example 18 matchstick equation

Matchsticks with n, the number of the form "A + B = C" equation can spell? Equation A, B, C match stick spell is an integer (if the number of non-zero, then the most significant bit is not 0). Spelling fight with a match stick figures 0-9 is shown in FIG.

 

Matchstick figure with FIG fight 0-9

Further, each of the equals sign and the plus requires two match stick.

Write a program, the number of input matchstick n, the number of output equations can spell different. Note: (1) if A ≠ B, then A + B = C and B + A = C as different equations (A, B, C> = 0); (2) A and B up to 3 digits ; (3) n all have to spend a match stick.

For example, input 18, output should be 9. That can spell matchstick 18 + 0 = 11 + 4 = 4,0 11,1 11,2 + 10 = 4 + 2 = 2 + 7 = 9,4 + 0 = 2 + 4,7 = 9, 1 = 10 + 0 = 11 + 11 and 11 nine equations.

        (1) 1 programming ideas.

        Save 0-9 match each number with the desired number of rods in an array, and further plus equals to four required.

        Write a function int needMatch (int num) statistics for the number of matchsticks num needed.

        2 cyclic procedures used for A (0 ~ 999) and B (0 ~ 999) a combination of the values ​​to be exhaustive, calls the function needMatch (A), needMatch (B) and needMatch (A + B) return the equation matchstick required number of three numbers, if needMatch (a) + needMatch (B) + needMatch (a + B) + 4 == n, the count.

        (2) a source.

#include <stdio.h>

int needMatch (int num); // number of matchsticks needed statistics num

int main ()

{

         int n,i,j,sum1,sum2,sum3,cnt;

         scanf("%d",&n);

         cnt=0;

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

            for (j = 0; j <1000; j ++) // 2 cyclic enumeration two addends

            {

                    sum1=needMatch(i);

                    SUM2 = needMatch (j);

                    sum3 = needMatch (i + j); // obtain two addends are required respectively and a match stick and

                    if(sum1+sum2+sum3+4==n)

                              cnt++;

            }

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

    return 0;

}

The number of matchsticks int needMatch // statistics (int num) num needed

{

    int table [10] = {6,2,5,5,4,5,6,3,7,6};

    int sum=0;

    if (num == 0) // 0 Number of special treatment

          return(6);

    else

    {

        while(num!=0)

         {

                   sum + = table [num% 10]; // number of decomposition matchstick each bit and adding this

                   num = num / 10; // preparation process at a

        }

        return(sum);

    }

}

        (3) 2 programming ideas.

        1 programming ideas needMatch call a function to return each number matches the number of rods num required when exhaustive, this function will be called 1000 * 1000 * 3 = 3 million (3 million), program execution slower. Following the method of space for time, increase the program execution speed.

       Since the number of equations that may occur between 0 ~ 1998, it is possible to define an array int needmatch [1999], the number of the matches stored spell each number from 0 to 1998 required, needmatch [i] value spell the number of the desired number matchstick i. Thus, after the first calculated value in each element of the array is good, and when the number of equations to be exhaustive A and B, the number of equations needed matchstick only three direct reference number, i.e., array element can. 1 corresponds to the idea of ​​needMatch function is only called 1999 times, so the running speed will be greatly improved.

        (4) 2 source.

#include <stdio.h>

int main ()

{

    int n,i,j,cnt;

    int match [10] = {6,2,5,5,4,5,6,3,7,6}; // defines the number of the matches for each number 0 to 9, the desired

    int needmatch [2000]; // save the number of the matches spell each number from 0 to 1999 required

         scanf("%d",&n);

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

             needmatch[i]=match[i];

    The number of each rod 10 to match the number of calculations required // 1999 for (i ++ i = 10;; i <2000)

     {

                   if (i <100) // 10 ~ 99 two-digit

                            needmatch[i]=match[i/10]+match[i%10];

                   else if (i <1000) // 100 ~ 999 three-digit

                            needmatch[i]=match[i/100]+match[i/10%10]+match[i%10];

                   else // 1000 ~ 1999 four-digit

                            needmatch[i]=match[i/1000]+match[i/100%10]+match[i/10%10]+match[i%10];

         }

    cnt=0;

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

       for (j = 0; j <1000; j ++) // 2 cyclic enumeration two addends

            if(needmatch[i]+needmatch[j]+needmatch[i+j]+4==n)

                     cnt++;

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

    return 0;

}

Exercise 18

18-1 Counting Problems

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

Title Description

Calculate all integer in the range 1 to n, the digital x (0 ≤ x ≤ 9) there was a total of how many times? For example, 1 to 11, in 1,2,3,4,5,6,7,8,9,10,11 i.e., number 1 appears four times.

Input Format

Two integers n, x, between separated by a space.

Output Format

An integer representing the number of times x appears.

SAMPLE INPUT

11 1

Sample Output

4

        (1) 1 programming ideas.

        Define an array count [10], the element count [0] ~ count [9] are used to save the number of all the n integer numbers 0-9. The initial value of the all-0, indicating the beginning of each number are unused.

        Program may be written in a cycle, the frame is:

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

       {

            For each integer i, the digits are sequentially separated k i, the corresponding count [k] ++;

        }

        For integer i, the digits of the separated operable to: continuously divided by 10, note the remainder, until the quotient is zero. The resulting residue sequence i is an integer from low to high of the digits. Specifically described as follows:

        while (i)

       {    count[ i %10] ++;

              i=i/10;

       }

       (2) a source.

#include <stdio.h>

int main ()

{

    int n,i,t,x;

    int count[10] = {0};

    scanf("%d%d",&n,&x);

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

    {

       t = i;

       while(t)

       {

             count[t%10]++;      t/=10;

         }

     }

    printf("%d\n",count[x]);

   return 0;

}

        (3) 2 programming ideas.

        Each of the digital program 1 n integer count cycle is completed with a double, n integers outer loop processing, the loop processing for each of the number n, the number of digits n of log 10 n. 1 +, so the time of the algorithm complexity is O (log n-* 10 n-).

        Below we give a more efficient method to solve this problem.

        1) consider all the bits by the n 0, 1, ..., 9 ten digits. A total of 10 of n from 0 to n. 9 n n-digits. In this 10 n n-bit number, 0,1,2, ..., 9 ten times using the same numbers, set f (n). f (n) satisfies the following recursive formula:

             

        2) For an m-bit integer, we can put between 0 to n n + 1 integers from small to large are arranged in such a way:

000……0

…………

099……9

100……0

…………

199……9

…………

        Such has been discharged natural number n. From 0 to 099 for 9 ...... this range, the most significant digits of blows not see its low position just m-1 is a 0 to m-1 th number 9 of 1 m-. Using the above recurrence formula, in this interval, the number of times each number appears (not including the most significant digit) is. Let n most significant digits is x, then the above-mentioned interval between the n total of x. Then x number of times each number appears to be complete statistics of these intervals. Look at the case of the highest digit, obviously 0 to x-1 times the figures reproduced in the most significant bit is, because an interval length; and x is the number of occurrences in the highest position. Next, that the number after removing the n most significant bits continue to repeat the above process. Until bits, you can complete the various figures of statistics.

        For example, for a digital 3482, we can be calculated from the number of all the numbers between 1 and 3482 each number appears.

        From 0 to 999, the number of times each number appears in this section may be used recursion formulas given earlier, i.e., 400 times each number appears. From 1000 to 1999, the intermediate is not removed kilobits 1, is a 000 to 999 are arranged, in this case, this interval from 0 to 3482 between a total of three. Therefore, from 0000 to 2999 between one thousand other outside, each number (0 to 9) the number of occurrences are 3 * 400.

        Then the statistical thousand figure, each interval length is 1000, so 0,1,2 each appeared in 1000 on one thousand. And 3 + 1 = 482 occurs 483 times.

        Then, discard thousands place, for 482, then using the above calculation method, the bits can be has been calculated.

        (4) 2 source.

#include <stdio.h>

#include <math.h>

int main ()

{

    int n,x,i,len,m,k,h;

    int pow10[10] = {1}, count[10] = {0};

    char d[11];

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

    {

        pow10[i] = pow10[i-1] * 10;

    }

    scanf("%d%d",&n,&x);

    len = log10 (n); // len indicates the current number bit weights, a 5-digit,

                            // highest authority is the fourth power of 10, len = 4

    m = len;

    sprintf (d, "% d", n); // convert the number n d of strings into an array

    k = 0; // k highest-order digits currently stored in the index array d

    h = d [k] - '0'; // h most significant digits of the current storage

    n% = pow10 [len]; // remove the highest bit of n

    while(len > 0)

     {

        if (h == 0) // if the current number is 0

        {

             count[0] += n + 1;       h = d[++k] - '0';

             just--; N% = pow10 [only];

             continue;

         }

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

            count [i] + = h * * only pow10 [en-1]; //

        for (i = 0; i <h; i ++) // highest bit 0 ~ h-1 Occurrences

            count[i] += pow10[len];

        count [h] + = n + 1; // number of occurrences of the most significant bit h

        just--; h = d [++ i] - '0';

        n% = pow10 [len]; // n highest discard

    }

    for (i = 0; i <= h; i ++) // a bit 0 ~ h Occurrences

        count[i] += 1;

   for (i = 0; i <= m; i ++) // 0 by subtracting the number of preamble

        count[0] -= pow10[i];

    printf("%d\n",count[x]);

   return 0;

18-2 trifecta

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

Title Description

The 1,2, ⋯, 9 9 number divided into three groups, each consisting of three digits 3 and 3 so that these constitute a three-digit number: 2: 3 ratio, satisfy the conditions of the test to obtain all three three digits.

Input Format

Do not enter

Output Format

Several rows of three numbers. Each row arranged in a numerical order.

SAMPLE INPUT

no

Sample Output

192  384  576

* * *

...

* * *

(Output being harmonized)

        (1) 1 programming ideas.

        Three three digits, a total of 9 bits, each bit can enumerate the number of objects, bit by bit to enumerate.

        9 integer variable definitions A, B, C, D, E, F, G, H, I 9 bits represent the number of three, each variable takes a value between 1 to 9. The ABC represents the number of 1 x, DEF represents a second number y, GHI 3 represents the number z.

        A ~ I The specific value can be calculated x, y, z, three required number satisfying the condition 2 * x == y && 3 * x == z (configuration 1: 2: 3 ratio); In addition, have to consider the number of each digit of the value is not the same as a ~ I, 9 variables to ensure that the value is different, as long as satisfying a + B + C + D + E + F + G + H + I == 45 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45) and A * B * C * D * E * F * G * H * I == 362880 (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 = 362 880) can.

        (2) a source.

#include <stdio.h>

int main ()

{

    int a,b,c,d,e,f,g,h,i,x,y,z; 

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

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

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

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

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

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

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

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

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

            {

              x=a*100+b*10+c;

              y=d*100+e*10+f;

              z=g*100+h*10+i;

              if (a+b+c+d+e+f+g+h+i==45  && a*b*c*d*e*f*g*h*i==362880

&& 2*x==y && 3*x==z)

                  printf("%d %d %d\n",x,y,z);

             }

    return 0;

}

        (3) 2 programming ideas.

        1 along the lines of the program, the number to be exhaustive 9 . 9 times, respectively, if the set number of three x, 2x and 3x, enumeration object at x, x is the minimum value of 123, the maximum value of 329 (since the next number 341 * 3 = 1023> 987), reduced exhaustive range of 107.

        Since the x-exhaustive, it is necessary on the respective bits of the digital three-digit three isolated. As nine numbers may be the same as the program 1, A ~ I a variable to hold nine. In Procedure 2, we used another method. Define a one-dimensional array a [9], the composition of the integer x, 2x, 3x nine numbers stored in the array a. Followed by a double loop 1-9 whether nine digital full statistics appear in the array.

        (4) 2 source.

#include <stdio.h>

int main ()

{

         int a[9],x,cnt,i,j,flag; 

         for (x = 123; x <= 329; x ++) // enumeration of all possible solutions

         {// The composition of the integer x, 2x, 3x nine numbers stored in the array a

        a[0]=x/100;  a[1]=x/10%10;  a[2]=x%10;

        a[3]=(2*x)/100;  a[4]=(2*x)/10%10;  a[5]=(2*x)%10;

        a[6]=(3*x)/100;  a[7]=(3*x)/10%10;  a[8]=(3*x)%10;

        cnt=0;

        for (i = 1; i <= 9; i ++) // Check whether 1-9 in nine figures in a

        {

                            flag=-1;

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

                                     if (i==a[j])

                                     {   flag=j; break;  }

                            if (flag!=-1)

                                     cnt++;

                            else

                                     break; // If there is not a digital, then exit the loop

         }

        if (cnt==9)

                printf("%d %d %d\n",x,x*2,x*3);

         }

         return 0;

}

18-3 trifecta (upgrade version)

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

Title Description

The 1,2, ..., 9 9 number into three groups, each consisting of three three-digit number, and that the three three-digit ratio is A: B: C, again determined to meet the conditions of all three three digits, if no solution, output "No !!!".

Input Format

Three numbers, ABC (A <B <C).

Output Format

Several rows of three numbers. Each row arranged in a numerical order.

SAMPLE INPUT

1 2 3

Sample Output

192  384  576

219  438  657

273  546  819

327  654  981

        (1) programming ideas.

         Are located three atoms x1, x2 and x3, to enumerate objects x1 is calculated x2 and x3 (x2 = b * x1 / a; x3 = c * x1 / a), and the minimum value of x1 123, a maximum of 987 * a / c (x3 maximum of 987).

         Due to be exhaustive x1, determines whether it is necessary 1 to 9 nine appeared in digital full x1, x2 and x3 of these three numbers. Defined for this purpose a hash array [10], wherein the hash [I] representing a digital value i (0 <= i <= 9) the number of occurrences of the three three-digit number, each element value of the front-exhaustive array of hash all zeros. If an exhaustive x1, hash [1] ~ hash [9] values ​​are all 1, then 1 to 9 nine appeared in digital full three numbers, to give a set of solutions.

        (2) source.

#include <stdio.h>

int main ()

{

         int hash[10],x1,x2,x3,a,b,c,i,flag=0;

         scanf("%d%d%d",&a,&b,&c);

         for (x1 = 123; x1 <= 987 * a / c; x1 ++) // enumeration of all possible solutions

         {  

                   for (i=0;i<=9;i++) hash[i]=0;

                   x2=b*x1/a;  x3=c*x1/a;

                  hash[x1/100]++;  hash[x1/10%10]++;  hash[x1%10]++;

                   hash[x2/100]++;  hash[x2/10%10]++;  hash[x2%10]++;

                   hash[x3/100]++;  hash[x3/10%10]++;  hash[x3%10]++;

                   for (i = 1; i <= 9; i ++) // Check whether this 1-9 digits appear 9

                            if (hash[i]!=1) break;

                   if (i>9)

                   {

                            printf("%d %d %d\n",x1,x2,x3);

                            flag=1;

                   }

         }

         if (flag==0)  printf("No!!!\n");

         return 0;

}

Guess you like

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