C language programming of 100 cases (9): physiological cycle

Example 9 Physiological period

Problem Description

Born with three menstrual cycles, respectively, the physical, emotional and intellectual cycles, their cycle length is 23 days, 28 days and 33 days. Each day is a cycle peak. At its peak this day, people will excel in the appropriate ways. For example, the peak period of intelligence, quick thinking people, concentration will be easier. Because different perimeter three cycles, it is usually the peak does not fall on the same day three cycles. For each person, we want to know when triple peak day.

For each period, we will be given from the first day of the current year, (not necessarily the first time and peak) in the days to peak. Your task is given a number of days from the first day of the beginning of the year the number of output next triple peak time of the day (from a few days at a given time) from a given start time (not including the given time). For example: given time is 10, the next triple peak appeared 12 days, the output 2 (note that this is not 3).

Input data

Input four integers: p, e, i, and d. p, e, i denote the time of the physical, emotional and intellectual cycles peak (time is counted from the first day of the year). d is a given time, it may be less than p, e, or i. All values ​​are non-negative and less than 365, less the time required 21252.

Output requirements

Starting from a given time, with the peak of the next three days (number of days from a given time).

SAMPLE INPUT

0 0 0 0

0 0 0 100

5 20 34 325

4 5 6 7

283 102 23 320

-1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days.

Case 2: the next triple peak occurs in 21152 days.

Case 3: the next triple peak occurs in 19575 days.

Case 4: the next triple peak occurs in 16994 days.

Case 5: the next triple peak occurs in 8910 days.

        (1) 1 programming ideas.

Assuming that from the first day of the year number, three k-day peak simultaneously. k meet the requirements of the problem must be greater than d, is less than equal to 21252 (23 × 28 × 33), and the following three conditions are satisfied:

1)(k-p) % 23 == 0

2) (k)% 28 == 0

3)(k-i) % 33 == 0

Interval of [d + 1,21252] k are each of the three conditions for determination, if the three conditions are satisfied, then k is the desired.

        (2) a source.

#include <stdio.h>

int main ()

{

    int p,e,i,d,caseNo = 0,k;

    while(scanf("%d%d%d%d",&p,&e,&i,&d) &&p!=-1)

    {

        ++caseNo;

        for(k = d+1;(k-p)%23!=0 || (k-e)%28!=0|| (k-i)%33!=0; k++);

        printf("Case %d: the next triple peak occurs in %d days.\n",caseNo,k-d);

    }

    return 0;

}

        (3) 2 programming ideas.

        Thinking 1 for the interval [d + 1,21252] k are each of the three conditions for determination, much overhead, it can be optimized.

        Specific Optimization method is: start interval [d + 1,21252] found in the first condition satisfies 1) physical and peak time k1, and from k1, k1 + 23, k1 + 2 * 23, k1 + 3 * 23 ... the first time looking for a satisfying the condition 2) emotional time and peak k2, of course, it must be a time of physical and peak; and finally k2, k2 + 23 * 28, k1 + 2 * 23 * 28, k1 + 3 * 23 * 28 ... Looking first time such a condition 3 is satisfied) time k3. The k3-d is the desired answer.

        (4) 2 source.

#include <stdio.h>

int main ()

{

    int p,e,i,d,caseNo = 0,k;

    while(scanf("%d%d%d%d",&p,&e,&i,&d) &&p!=-1)

    {

        ++caseNo;

        for (k = d + 1; (kp)% 23; k ++); // Enumeration peak strength

        while (! (ke)% 28 = 0) k + = 23; // enumeration emotional peak

        while (! (ki)% 33 = 0) k + = 23 * 28; // find three peaks

        printf("Case %d: the next triple peak occurs in %d days.\n",caseNo,k-d);

    }

    return 0;

}

Problem 9

9-1 Coin Program

Problem Description

50 coins, may include four types: one, five angle, angle 1 and 5 minutes.

The total value of the known 50 coins 20 yuan, and quantity of various coins.

For example: 2,34,6,8 is a kind of program. The 2,33,15,0 is another possible scenario, the program is clearly not unique.

Write programs to obtain different scenarios like this, a total of how many?

Input data

no

Output requirements

All the possible scenarios, see sample output format output.

SAMPLE INPUT

No Input

Sample Output

1: 0 , 38 , 8 , 4

2: 1 , 36 , 7 , 6

3: 2 , 33 , 15 , 0

……

        (1) programming ideas.

        Directly to the number of the four types of coins to be exhaustive. Wherein one yuan most 20, at most 40 angle 5, an angle up to 50, up to 50 5 minutes.

        Further, if the unit of cell, then 50 cents, 10 cents, 5 Branch floating-point data into, easily calculated error. 1 million, 5 angle, angle 1, 5 may be divided into 100 points, 50 points, 10 points and 5 points, so that the entire process using the integer data.

        (2) source.

#include <stdio.h>

int main ()

{

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

    for(a=0;a<=20;a++)

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

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

       for(d=0;d<=50;d++)

       {

                     if(a*100+b*50+c*10+d*5==2000 && a+b+c+d==50)

                     {

                            printf("%d: %d , %d , %d , %d\n",++cnt,a,b,c,d);

                     }

        }

    return 0;

        (3) exhaustive optimization.

        The above program using brute-force method to solve relatively simple. However, there is room for improvement and optimization in the structure provided exhaustive, like exhaustive selection parameters.

        In general, when the problem solved by the exhaustion method, can be optimized considered from two aspects.

        1) establishment of simple mathematical models.

        The mathematical model to minimize the number of variables, independent of each other between them. The dimension of the search space solutions of this problem is small. The reaction to the program code, a nested loop level less. For example, in the above procedure, using the variables a, b, c, d represent 1 million, 5 angles, and 1 angle requisite number of coins of 5, these four variables to be exhaustive, circulating levels of four layers. In fact there are four variables to each other two constraint conditions, the number of pieces or equal to 50, or a total value of 20. Thus, three variables can only be exhaustive, another variable determined by the constraints so as to reduce the circulating levels of three layers.

        2) reducing the search space.

        Use of existing knowledge, narrowing the range of each variable mathematical model, to avoid unnecessary calculations. The reaction to the program code, the number of times the loop is executed is reduced. For example, when an exhaustive, consider $ 1 number of pieces a, a maximum of 20 (i.e., 0 <= a <= 20), and then consider the number of pieces b 5 angles, the use of a total value of no more than $ 20 constraint, it number of pieces up to (2000-a * 100) / 50 pieces (i.e., 0 <= b <= (2000-a * 100) / 50), after considering the number of pieces c 1 angle, which number of pieces up to (2000-a * 100 -b * 50) / 10 (i.e., 0 <= c <= (2000-a * 100-b * 50) / 10). Such an exhaustive number of cycles will be greatly reduced.

        After the above-described idea source optimization follows.

#include <stdio.h>

int main ()

{

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

    for(a=0;a<=20;a++)

     for(b=0;b<=(2000-a*100)/50;b++)

      for(c=0;c<=(2000-a*100-b*50)/10;c++)

        {

                     d = (2000-a * 100-b * 50-c * 10) / 5; // the rest is filled with coins of 5

                     if(a+b+c+d==50)

                     {

                            printf("%d: %d , %d , %d , %d\n",++cnt,a,b,c,d);

                     }

        }

    return 0;

}

        The total number of medals can be used not more than 50 constraints. Consider 1 per number of pieces a, a maximum of 20 (i.e., 0 <= a <= 20), and then consider the number of pieces b 5 angle, its number of pieces up to (50-a) gold (i.e., 0 <= b <= (50-a), after considering the number of pieces of angle c 1, which is the maximum number of pieces (50-ab) pieces (i.e., 0 <= c <= 50-ab). in this source program optimization idea as follows.

#include <stdio.h>

int main ()

{

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

    for(a=0;a<=20;a++)

     for(b=0;b<=50-a;b++)

      for(c=0;c<=50-a-b;c++)

            {

                     d = 50-abc; // the rest is filled with coins of 5

                     if(100*a+50*b+10*c+5*d==2000)

                     {

                            printf("%d: %d , %d , %d , %d\n",++cnt,a,b,c,d);

                     }

            }

    return 0;

}

9-2 and the triangular plot

Problem Description

S is a positive integer and the eight positive integers mutually unequal number 8 filled triangle (shown in FIG. 1), if the number of the three sides of the triangle are equal and the numbers and the product of the three sides are equal, the triangle and the product is called a triangle.

 

FIG digital triangle 1

For example, and 45 and the product of the triangle shown in FIG.

FIG 2 s = 45 and the product of the triangle

Write a program, and a product output of s triangle.

Input data

A positive integer S (36≤S≤300).

Output requirements

S and all of the triangle and the product, the required output of the program will not be repeated. As shown in FIG. 2, 8 and 9, the exchange, or the exchange 3 and 4, or simultaneously with the exchange 9 and 12 4,8 and 3,2, 3 kinds of programs obtained deemed embodiment of FIG 2 is given the same Program.

SAMPLE INPUT

45

Sample Output

1:2 , 8 , 9 , 1 , 4 , 3 , 12 , 6 ,   s1=20, s2=144

Explanation

Control data in FIG. 2, the sample note in the output order Experience number 8, and the other is a value representing each side of the integer s1 and the value s2 represent integers each side of the product.

        (1) programming ideas.

        Follow the instructions of the sample output, digital triangle shown in FIG. 8 provided number distribution as shown in Figure 3.

        Since both waist triangle can be exchanged with each other, to avoid duplication, the digital triangle might convention "big small, the left and right small large", i.e. b1 <b7, b2 <b3 and b6 <b5.

 

Figure 3 a schematic view of a triangular profile

        Thus, exploration of the cycle may be b1, b7 values ​​according to the conventions provided:

        b1 is in the range of 1 ~ (s-21) / 2; (b1 by addition, B7, the sum of the number of at least 6 for the other 21)

        B7 range is b1 + 1 ~ (s-28); (except due to b7, the sum of the number of at least 7 other 28)

        B4 is in the range of 1 ~ (s-28); (by addition b4, 7 is at least the sum of the number of the other 28)

        Similarly, convention b2 <b3, b6 <b5, may be provided:

b2 is in the range of 1 ~ (s-21) / 2; (by addition b2, b3, 6 is at least the sum of the number of the other 21)

The range of b3 b2 + 1 ~ (s-28);

B6 in the range of 1 ~ (s-21) / 2; (except by b5, b6, 6 is at least the sum of the number of the other 21)

b5 ranges b (6) +1 ~ (s-28);

b8 = s- (b1 + b2 + b3 + b4 + b5 + b6 + b7)

Eight integers taken, requires the following four tests:

1) If b8 <= 0, not meet the requirements;

2) the number of 8 when this occurs the same number, not meet the requirements;

3) If the three sides and unequal, not to meet the requirements;

4) If the product ranging from three sides, not meet the requirements.

8 If a number of the above four detection, that is a solution, printout, and counts the number of solutions.

        Whether due to the need for the same number appeared to detect 8 integers, 8 thus can be saved in a number of one-dimensional array, one-dimensional array defined int b [9]; wherein the array element b [1] ~ b [8] respectively in FIG. 3 b1 ~ b8.

        The overall program can be written as a Seventh-loop structure, as follows:

    for(b[1]=1;b[1]<=(s-21)/2;b[1]++)

     for(b[7]=b[1]+1;b[7]<=s-28;b[7]++)

      for(b[4]=1;b[4]<=s-28;b[4]++)

       for(b[2]=1;b[2]<=(s-21)/2;b[2]++)

        for(b[3]=b[2]+1;b[3]<=s-28;b[3]++)

         for(b[6]=1;b[6]<=(s-21)/2;b[6]++)

          for(b[5]=b[6]+1;b[5]<=s-28;b[5]++)

                     {

                            The exhaustive number 8, for four detecting, determining whether a set of solutions;

                    }

        4 test, in addition to more complex if the same number appears in the outer inspection number 8, and the other are determined to a simple calculation.

        To detect whether the same number appears in the number of 8, a flag may be set to t = 0; then turn number per each of its number is compared with the cycle, if the same occurs, then t = 1 is set and exits cycle.

        After the end of the execution cycle, if t == 1, then the number 8 appears in the same number; t if the initial set value 0 remains, then the same number does not exist in the number 8. Algorithm described as:

      t=0;

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

             for(j=i+1;j<=8;j++)

                         if(b[i]==b[j])

                          { 

                                     t=1; i=7; break;

                           }

        (2) a source.

#include <stdio.h>

int main ()

{

    int i,j,t,s,s1,s2,cnt,b[9];

    scanf("%d",&s);

    cnt=0;

    for(b[1]=1;b[1]<=(s-21)/2;b[1]++)

     for(b[7]=b[1]+1;b[7]<=s-28;b[7]++)

      for(b[4]=1;b[4]<=s-28;b[4]++)

       for(b[2]=1;b[2]<=(s-21)/2;b[2]++)

        for(b[3]=b[2]+1;b[3]<=s-28;b[3]++)

         for(b[6]=1;b[6]<=(s-21)/2;b[6]++)

          for(b[5]=b[6]+1;b[5]<=s-28;b[5]++)

            {

                              b[8]= s-(b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]);

                              if(b[8]<=0)  continue;

                              t=0;

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

                                 for(j=i+1;j<=8;j++)

                                        if(b[i]==b[j])

                                        {  t=1; i=7; break; }

                           if(t==1)  continue;

                           s1= b[1]+b[2]+b[3]+b[4];

                           if(b[4]+b[5]+b[6]+b[7]!=s1 || b[1]+b[8]+b[7]!=s1)

                                     continue;

                           s2=b[1]*b[2]*b[3]*b[4];

                          if(b[4]*b[5]*b[6]*b[7]!=s2 || b[1]*b[8]*b[7]!=s2)

                                     continue;

                         cnt++;

                         printf("%d : ",cnt);

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

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

                         printf("  s1=%d, s2=%d\n",s1,s2);

            }

    return 0;

             }

        (3) exhaustive optimization ideas.

        Although the above exhaustive programming feasible. However, the speed of this process is too slow. For example, s = 45 in the program changed s = 89, i.e., integer calculations and 8 and 89 consisting of a triangular plot, the program runs, the results obtained is shown below.

1 : 6 , 14 , 18 , 1 , 9 , 8 , 21 , 12 ,   s1=39, s2=1512

2 : 8 , 12 , 15 , 1 , 16 , 9 , 10 , 18 ,   s1=36, s2=1440

3 : 8 , 4 , 27 , 2 , 12 , 3 , 24 , 9 ,   s1=41, s2=1728

4 : 15 , 9 , 16 , 1 , 12 , 10 , 18 , 8 ,   s1=41, s2=2160

        Solutions of the above four procedures are required to wait a long time. In order to improve computational efficiency, must be optimized for the program, you can start from the loop settings. Specific ideas are:

       1) increase the s + b1 + b7 + b4 is a multiple of 3 is detected.

        Because the three vertices of the triangle element in the calculation of the three sides of each calculated twice, i.e., s + b1 + b7 + b4 = 3 * s1, then b1, b4, b7 increased circulation s + b1 + b7 + b4 if detecting divisible by 3.

        If the (s + b1 + b7 + b4)% 3 ≠ 0, directly continue, continue new b1, b4, b7 exploration, without exploring the back b2, b3, b5 and B6;

        Otherwise, denoted s1 = (s + b1 + b7 + b4) / 3, to explore down.

        2) streamline the cycle, the cycle streamline the Seventh Five.

        The convention for exploration cycle retention b1, b7, and b4 is a value, set op. To b2, b3, b5 and b6 exploration cycle optimization. Can explore value b3, b5 according to the conventions provided:

b3 is in the range of (s1-b1-b4) / 2 + 1 ~ s1-b1-b4; NOTE: s1 = (s + b1 + b7 + b4) / 3

b5 ranges (s1-b4-b7) / 2 + 1 ~ s1-b4-b7;

Meanwhile The sum of each side s1, calculated b2, b6, and b8, i.e.

         b2 = s1-b1-b4-b3

         b6 = s1-b4-b5-b7

         b8=s1-b1-b7

       In this way, while also streamlining is positive detection on b8, also streamlined the trilateral and equality testing. B only detect whether there is the same positive integer array with the same volume whether to trilateral.

        (4) the improved source.

#include <stdio.h>

int main ()

{

    int i,j,t,s,s1,s2,cnt,b[9];

    scanf("%d",&s);

    cnt=0;

    for(b[1]=1;b[1]<=(s-21)/2;b[1]++)

     for(b[7]=b[1]+1;b[7]<=s-28;b[7]++)

      for(b[4]=1;b[4]<=s-28;b[4]++)

        {

                  if((s+b[1]+b[4]+b[7])%3!=0)

                            continue;

                  s1=(s+b[1]+b[4]+b[7])/3;

                 for(b[3]=(s1-b[1]-b[4])/2+1;b[3]<s1-b[1]-b[4];b[3]++)

                     for(b[5]=(s1-b[4]-b[7])/2+1;b[5]<s1-b[4]-b[7];b[5]++)

                      {

                              b[2]=s1-b[1]-b[4]-b[3];

                               b[6]=s1-b[4]-b[7]-b[5];

                              b[8]=s1-b[1]-b[7];

                              t=0;

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

                                 for(j=i+1;j<=8;j++)

                                        if(b[i]==b[j])

                                       { t=1;  i=7; break; }

                             if(t==1)  continue;

                             s2=b[1]*b[2]*b[3]*b[4];

                            if(b[4]*b[5]*b[6]*b[7]!=s2 || b[1]*b[8]*b[7]!=s2)

                                   continue;

                           cnt++;

                          printf("%d : ",cnt);

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

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

                         printf("  s1=%d, s2=%d\n",s1,s2);

                     }

           }

    return 0;

}

        Run the above procedures to improve exhaustive, when s = 89 and the solution obtained before the same, but the time is greatly reduced.

9-3 perfect expression

Problem Description

The numbers 1, 2, ..., 9 which fill in the following nine digits integrated arithmetic expression containing the power 9 of the □, so that the following holds

         □^□+□□÷□□-□□×□=0  

Requires a digital 1,2, ..., 9 9 digits which appear in formula once and only once.

Input data

no

Output requirements

Fill in all possible ways the output, the output format see sample output.

SAMPLE INPUT

no

Sample Output

1:3 ^ 5 + 87 / 29 - 41 * 6=0

 ……

        (1) 1 programming ideas.

        Provided Formula 6 from left to right are integers a, b, x, y, z, c, wherein x, y, z is an integer of 2, in the range of 12 ~ 98; a, b, c is a an integer ranging from 1 to 9.

        Setting a, b, c, x, y, z cycle, each set of exhaustive a, b, c, x, y, z, for the following tests:

        1) If x is not a multiple of y, i.e. x% y! = 0, then return to continue to the next to be exhaustive.

        2) If the equation does not hold, i.e., a ^ b + x / yz * c! = 0, then return to continue to the next to be exhaustive.

3) whether the digital presence 9 wherein like numerals. 6 wherein the integer 9 digits separated, respectively assigned to the array element f [1] ~ f [9]. [0] = 0 F in comparison along with additional (to ensure nine digits are not zero) in a total of 10 digits by one double loop.

If the presence of the same number, t = 1, it is not a solution, continue to the next to be exhaustive.

If the presence of the same number, i.e., where nine digits 1 to 9 do not overlap, hold mark t = 0, is a set of solutions, the resultant output perfect expression. The number and statistical solution of n.

      (2) a source.

#include <stdio.h>

int main ()

{

    int a,b,c,x,y,z;

    int i,j,k,t,n,f[10];

    n=0;

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

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

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

       for(x=12;x<=98;x++)

        for(y=12;y<=98;y++)

         for(z=12;z<=98;z++)

         {

                   if (x%y!=0) continue;

                    k=1;

                    for (i=1;i<=b;i++)     // 计算k=a^b

                       k=a*k;

                   if(k+x/y-z*c!=0) continue;

                   f[0]=0;

                  f [1] = a; f [2] = b; f [3] = c; // 9 array numbers that are assigned to f

                  f[4]=x/10; f[5]=x%10;

                  f[6]=y/10; f[7]=y%10;

                  f[8]=z/10; f[9]=z%10;

                   t=0;

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

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

                         if(f[i]==f[j])

                         {T = 1; break;} // test whether the digital duplication

                    if(t==0)

                    {

                        n ++; // output of a solution, with n the number of statistical

                        printf("%d:%d ^ %d + %d / %d - %d * %d=0\n",n,a,b,x,y,z,c);

                    }

           }

    return 0;

}

         (3) 2 programming ideas.

        For the above program optimization.

        Due to the requirement for the overall expression: a ^ b + x / yz * c = 0, then, x = (z * ca ^ b) * y. Thus may be provided a, b, c, y, z cycle, each set of exhaustive a, b, c, y, z, calculate x. Such treatment may be omitted x cycle, while omitting x is divisible by y, the equation is established detection be omitted.

        After calculation of x, it is sufficient to detect whether the two digits of x. If the calculated integer x is not two, the process returns to continue to the next to be exhaustive.

        Further, Formula 9 digits whether the same numbers employed a method:

        F defines an array of six times nine integer numbers separated occurrence statistics, i.e., the digital value of the formula f [i] i in number, initial value 0 is assigned full. After the statistics, if a f [i] (i = 1 ~ 9) is not 1, the number is not satisfied certain 1,2, ..., 9 nine numbers appear once and only once throughout labeled t = 1, Solutions not, returns to continue next to be exhaustive; if all f [i] are all 1, number satisfying 1,2, ..., 9 nine numbers appear once and only appears once, hold mark t = 0, a solution, the resulting output perfectly integrated expression.

        (4) 2 source.

#include <stdio.h>

int main ()

{

    int a,b,c,x,y,z;

    int i,k,t,n,f[10];

    n=0;

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

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

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

        for(y=12;y<=98;y++)

         for(z=12;z<=98;z++)

          {

                 k=1;

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

                        k=a*k;

                x=(z*c-k)*y;

                if(x<10 || x>98)  continue;

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

                        f[i]=0;

                f [a] ++; // number of times recording each occurrence of 9 digits; f [b] ++; f [c] ++

                f[x/10]++;  f[x%10]++;   f[y/10]++;  f[y%10]++;

                 f[z/10]++;  f[z%10]++;

                 t=0;

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

                    if(f[i]!=1)

                    {T = 1; break;} // test whether the digital duplication

              if(t==0)

                {

                         n++;

                        printf("%d:%d ^ %d + %d / %d - %d * %d=0\n",n,a,b,x,y,z,c);

                 }

           }

     return 0;

Guess you like

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