C language programming of 100 cases (15): division algorithm

Example 15 division equation

Problem Description

Enter a positive integer n (2≤n≤68), in ascending form expressions such as the outputs of all abcde / fghi = n a. Wherein an arrangement of a ~ i 1 to 9.

Input Format

Each row a positive integer n (n <= 1500), n = 0 input end.

Output Format

Meet the conditions of all the output of the form of expression abcde fghi = n /, each expression per line, see the specific format of the sample output.

SAMPLE INPUT

4

20

62

0

Sample Output

15768/3942=4

17568/4392=4

23184/5796=4

31824/7956=4

No Solution!

79546/1283=62

94736/1528=62

(1) programming ideas.

In this case need to determine a good idea exhaustive. Although the subject of a said a ~ i 1 to 9 are arranged, are arranged to be exhaustive but all having 1 to 9 clearly not necessary.

FGHI be exhaustive divisor, which is a 4-digit, minimum 1234, 9876 may be a maximum, and then press fghi * n calculated ABCDE, and finally determines whether the nine numbers are not the same.

To determine nine numbers are the same, can define an array flag [10], wherein In Flag [i] value represents the number of digital i appears in the equation, it is clear flag [1] ~ flag [9] the value of all 1 only fulfil requirements.

Further, when properly optimized be exhaustive. If less than 12 345 abcde calculated apparent fghi small divisor, the divisor is increased directly for the next exhaustive; calculated if greater than 98 765 abcde apparent divisor fghi too, is no longer possible to find the solutions, exit loop exhaustive.

(2) source.

#include <stdio.h>

int main ()

{

    int n,x,y,i,flag[10],t;

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

    {

        t=0;

        for (y=1234;y<=9876;y++)

        {

            x = y * n;

            if (x<12345) continue;

            if (x>98765) break;

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

               flag[i]=0;

            flag[x/10000]++; flag[x%10000/1000]++;

            flag[x%1000/100]++; flag[x%100/10]++;

            flag[x%10]++;

            flag[y/1000]++; flag[y%1000/100]++;

            flag[y%100/10]++; flag[y%10]++;

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

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

            if (i==10)

            {

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

                t++;

            }

        }

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

    }

    return 0;

}

Exercise 15

15-1 perfect cube

Problem Description

A . 3 + B . 3 + C . 3 = D . 3 is a perfect cubic equation. For example, 1 . 3 + 6 . 3 + 8 . 3 = 9 . 3 . All four-tuple (a, b, c, d ) write a program, the output of less than 100, such that A . 3 + B . 3 + C . 3 = D . 3 , wherein 1≤a <b <c <d≤100.

Input Format

No Input

Output Format

All meet within 100 A . 3 + B . 3 + C . 3 = D . 3 four-tuple (a, b, c, d ) , and each output line 5 groups.

SAMPLE INPUT

No Input

Sample Output

(  3,  4,  5,  6)  ( 1, 6,  8,  9)  ( 6,  8, 10, 12)  (  2, 12, 16, 18)  (  9, 12, 15, 18)

(  3,  10, 18, 19)  ( 7, 14, 17, 20)  ( 12, 16, 20, 24)  (  4, 17, 22, 25)  (  3, 18, 24, 27)

……

(1) programming ideas.

Because satisfy all requirements within 100 A . 3 + B . 3 + C . 3 = D . 3 four-tuple (a, b, c, d ) , and thus define an array int cube [101] ;, and cube [i] values Fu I . 3 , for a direct reference to the rear.

Starting from exhaustive d, exhaustive of the scope of the

6≤d≤100

1≤a≤d-3

a+1≤b≤d-2

b+1≤c≤d-1

(2) source.

#include <stdio.h>

int main ()

{

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

    int cube [101];

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

       cube [i] = i * i * i;

    for (d=6 ; d<=100; d++)

       for (a=1; a<d-2; a++ )

       {

          if (cube [d] <cube [a] + cube [a + 1] + cube [a + 2]) break; // no need to continue searching b and c

          for (b=a+1 ; b<d-1; b++)

          {

              if (cube [d] <cube [a] + cube [b] + cube [b + 1]) break; // no need to continue searching c

              for (c=b+1; c<d;  c++)

                 if (cube[d]==cube[a]+cube[b]+cube[c])

                  {

                                cnt++;

                                printf("(%3d,%3d,%3d,%3d)  ",a,b,c,d);

                                if (cnt%5==0) printf("\n");

                  }

         }

    }

    return 0;

}

15-2 score Split

Problem Description

Enter a positive integer k, find all positive integers x≥y, so that the 1 / k = 1 / x + 1 / y.

Input Format

Test input comprising a plurality of sets of data, each row is a positive integer k.

Output Format

The number of the output data of each of the first solution, and then outputs all of the solution, not a solution per line. See the specific format of the sample output.

SAMPLE INPUT

2

12

Sample Output

2

1/2=1/6+1/3

1/2=1/4+1/4

8

1/12=1/156+1/13

1/12=1/84+1/14

1/12=1/60+1/15

1/12=1/48+1/16

1/12=1/36+1/18

1/12=1/30+1/20

1/12=1/28+1/21

1/12=1/24+1/24

(1) programming ideas.

At first glance it seems exhaustive range can not be determined, but because of x≥y, there are 1 / x≤1 / y,

Thus the 1 / k = 1 / x + 1 / y found 1 / y = 1 / k-1 / x≥1 / k-1 / y, so that 2 / y≥1 / k i.e. y≤2k. Of course y≥k + 1. So long as exhaustive y is in the range of k + 1 ~ 2k, and then try to x is calculated according to y.

(2) a source.

#include <stdio.h>

int main ()

{

    int k;

    while (scanf("%d",&k)!=EOF)

    {

        int x, y, cnt = 0;

        for (y=k+1;y<=2*k;y++)

        {

            if(k*y%(y-k)==0)

            {

                cnt++;

            }

        }

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

        for (y=k+1;y<=2*k;y++)

        {

            if(k*y%(y-k)==0)

            {

                x=k*y/(y-k);

                printf("1/%d=1/%d+1/%d\n",k,x,y);

            }

        }

    }

    return 0;

}

       (3) the source 2.

        In source 1, exhaustive cycle twice, with a determined number of cycles of the solution, with another case where each cycle of the output solution. Obviously, the solution can be used to save the situation will be determined at the time of the array with the number of cycles to solve down, so do not solve the cycle again, the situation is saved by an array of solution can be directly output.

#include <stdio.h>

int main ()

{

    int k;

    while (scanf("%d",&k)!=EOF)

    {

        int cnt=0;

        int t;

        int x[2*k],y[2*k];

        for (t=k+1;t<=2*k;t++)

        {

            if(k*t%(t-k)==0)

            {

                x[cnt]=k*t/(t-k);

                y [cnt] = t;

                cnt++;

            }

        }

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

        for (t=0;t<cnt;t++)

        {

            printf("1/%d=1/%d+1/%d\n",k,x[t],y[t]);

        }

    }

    return 0;

}

        Note: In the above source 2, x and y are variable length arrays array definition, the current C standard support such usage.

15-3 one number three square

Problem Description

There is a six-figure category, which itself is only a few square feet, and its first three and the last three are also square numbers, such numbers are called the "three square of a number."

Input Format

No Input

Output Format

All outputs a square of the number three. Each line of output a square of a number three, the specific format see sample output.

SAMPLE INPUT

No Input

Sample Output

144400 : 12*12=144,20*20=400,380*380=144400

225625 : 15*15=225,25*25=625,475*475=225625

……

  (1) programming ideas.

If the program is exhaustive of all six figures (100000 to 999999), to determine whether this is a six-digit number three square, apparently too much trouble.

Since a "a number of three squares" number, which is the square of the first three and the last three digits must, therefore, be determined first of all within the square of the number of 999, a maximum of only 32 (i.e., the square of 0 to 31 square, more than 102,432 square 3). Define an array int a [32] 32 to hold this number of squares.

After this six-digit square 32 square of two each of the program is determined to be exhaustive, obviously must be high three array A [10] (i.e., not less than 10 square the first digit is not 0 100 only) number. Algorithm described as:

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

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

                   {

                            c = 1000 * a [i] + a [j]; // a [i] as a high three, a [j], as the lower three bits constituting the six-digit

                            if (c is the number of square)

                                 Count information and corresponding output

                   }

       (2) source.

#include <stdio.h>

#include <math.h>

int main ()

{

         int a[32],i,j;

         long b,c,t;

         for (i = 0; i <= 31; i ++) // number of squares of all the statistics from 0 to 999 of the

                   a[i]=i*i;

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

         {

                   b = 1000 * a [i]; / * high three-digit * /

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

                   {

                            c = b + a [j]; / * six-digit * /

                            t = sqrt (c); / * * square root six-digit /

                            if (c == t * t) / * is determined whether or not the six-digit number of squares * /

                            {

                                     printf("%d : %d*%d=%d,%d*%d=%d,%d*%d=%d\n",c,i,i,a[i],j,j,a[j],t,t,c);

                            }

                   }

         }

    return 0;

}

Guess you like

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