Cainiao Qifang Data Structure series (1): advanced applications of one-dimensional arrays-a single query that can accommodate over multiple digits! Program!

Source: Internet
Author: User
Qifang is determined to change the old cainiao image. He also thought about prawn addiction. He recently joined the game again and studied "design patterns" and "data structures" behind closed doors. He also hoped to take care of the old birds! Let old dishes become old birds as soon as possible! * ^_^ *

The usage of one-dimensional arrays is also found. You can refer to the following questions:

Textbook: data structure, in C language, published by Tsinghua University Press

Question:Design a n that can accommodate multiple digits! OfProgram.

Concept:

1. n! For example, 1! = 1*1; 2! = 2*1; 3! = 3*2*1... and so on.

2. Declare a large array of data [] to store the calculated results, because any existing numeric class cannot store such long or longer calculation results.

3. Let's study a rule:

A, 1! = 1*1 = 1

2! = 1 = 2

3! = 3*2 = 6

4! = 4*6 = 24

5! = 5*24 = 120

// The Key to this step is described below:

// Array: 4 3 2 1 4 3 2 1

// Value: 0 0 2*5 4*5 is equivalent to 24*5, for example: 0 0 10 20

// When we store only one numeric value for each array, we have to move more than 10 values in the existing array to one, and the result is changed:

// Array: 4 3 2 1

// Value: 0 1 2 0

// The last step is to print out the arrays with values in the Number Group cyclically and sort them with the highest index value!

B. The specific idea is: first, make the data array an initial value. Set the first value to 1 and the number of digits to 1. Then, the product of each multiplication is stored in the array. And cyclically process the number of more than 10 in each array. If the number is greater than 10, just add 1 to the number of digits and divide the original number by 10, after adding the values of the previous digit, the Business digit is stored in the array of the previous digit, and the remainder is stored in the array of the original digit. Finally, the values in the number group are printed based on the current number of digits. ----- This section is taken from the book

First read Code You will understand:
/**/ ///   <Summary>

/// Calculate n! Program

///   </Summary>

///   <Param name = "length"> Size of the calculated result Array </Param>

///   <Param name = "Number"> N Value </Param>

Private   Void Method1 ( Int Length, Int Number)

{

Int [] Data =   New   Int [Length];

Int Digit =   1 ; // The position of the current array, starting from 1.

Int I, j, R, K;



Try

{

// Assign an initial value to the array. All values are 0.

For (I = 1 ; I < Length; I ++ )

{

Data [I]= 0;

}


Data [ 0 ] =   1 ;

Data [ 1 ] =   1 ;


For (I = 1 ; I < Number; I ++ )

{

// Calculate the current N value

For (J = 1 ; J <= Digit; j ++ )

Data [J] * = I;


For (J = 1 ; J <= Digit; j ++ )

{

// When the value in the array is greater than 10,



If (Data [J] >   10 )

{

For (R = 1 ; R <= Digit; R ++ )

{

If (Data [digit] > 10 )

Digit ++ ; // Array subscript plus 1


// Value of the array of the previous digit = value of the array of the previous digit + value of this array/10

Data [R + 1 ] + = Data [R] /   10 ;

// The value of this array = the value of this array/10, and then the remainder

Data [R] = Data [R] %   10 ;

}

}

}


Console. Write (I. tostring () +   " ! = " );

// Print N in a loop based on the current value of the array! .

For (K = Digit; k > 0 ; K -- )

Console. Write (data [k]);

Console. Write ( " \ R \ n " );

}

}

Catch (Exception ex)

{

Console. writeline (ex. Message );

}

}

5. Call method: Method1 (200,100); // calculate 100! Result: 195662859738407857961118354931666080074342886499720644995417374332204872604804112678108355178695515232661307223758251185210916864000000000000000000000000

There are hundreds of thousands!

6. cainiao has poor organizational skills. If you don't understand it, you can send comments and ask the old birds to help you .....

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.