Common algorithms-push Method

Source: Internet
Author: User

 

The progressive method is a method that uses the recursive relationship of the question itself to solve the question. If n = 1, the solution is known or can be easily obtained. The problems that can use the progressive method to construct the algorithm have an important recursive nature, that is, when the problem scale is the solution of the I-1, from the recurrence nature of the problem, can be obtained from the scale of 1, 2, ..., A series of solutions of I-1, the structure of the problem scale for the I solution. In this way, the program can start from I = 0 or I = 1, repeatedly, from known to the I-1 scale of the solution, through recursion, to obtain the scale of the I solution, until the solution with a scale of N is obtained.

[Problem] factorial calculation

Problem description: write a program to calculate and output the factorial K of K for the given n (n ≦ 100! (K = 1, 2 ,..., N.

Because the required integer may be much larger than the digits of a general integer, the program uses a one-dimensional array to store long integers. Each element of a long integer array stores only one digit of a long integer. Store M-bit integer N in array:

N = A [m] × 10m-1 + a [M-1] × 10m-2 +... + A [2] × 101 + A [1] × 100

Use a [0] to store the M digits of the long integer N, that is, a [0] = m. As agreed above, each element of the array stores the factorial K of K! The second and third elements of the array from the low to the high ....... Example: 5! = 120, the storage format in the array is:

3 0 2 1 ......

The first element 3 indicates that the long integer is a three-digit number, followed by 0, 2, and 1 from the low position to the high position, and expressed as an integer 120.

Calculate the factorial K! Can use the obtained factorial (k-1 )! After the continuous accumulation of K-1 obtained. For example, known 4! = 24, calculate 5 !, You can add four times to the original 24 and then get 120. For details, see the following procedure.

# Include <stdio. h>

# Include <malloc. h>

# Define maxn1000

Void pnext (int A [], int K)

{Int * B, M = A [0], I, j, R, carry;

B = (int *) malloc (sizeof (INT) * (m + 1 ));

For (I = 1; I <= m; I ++) B =;

For (j = 1; j <= K; j ++)

{For (carry = 0, I = 1; I <= m; I ++)

{R = (I <A [0]? A + B: a) + carry;

A = R % 10;

Carry = r/10;

}

If (carry) A [++ m] = carry;

}

Free (B );

A [0] = m;

}

 

Void write (int * a, int K)

{Int I;

Printf ("% 4D! = ", K );

For (I = A [0]; I> 0; I --)

Printf ("% d", );

Printf ("/n ");

}

 

Void main ()

{Int A [maxn], N, K;

Printf ("Enter the number N:");

Scanf ("% d", & N );

A [0] = 1;

A [1] = 1;

Write (A, 1 );

For (k = 2; k <= N; k ++)

{Pnext (A, K );

Write (A, K );

Getchar ();

}

}

 

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.