Write the algorithm step by step (n! Number of zeros at the end of the column)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

N for many interview questions! The number of zeros in the result is also a common problem. So what is the solution to this question? I would like to share with you some of my views here. If you have any opinions, please give me more comments.

 

N! The number of zeros mainly lies in whether the multiplier can be divisible by 2 and 5, as long as the multiplier can be found by 2 and 5 integers, so my code flow is like this:

 

(1) check whether the integer in the current data can be divisible by 2, and modify the integer value.

 

(2) check whether the current data can divide the integer by 5 and modify the integer value

 

(3) If the conditions 1 and 2 meet the same time, it indicates that there is already zero, count ++

 

(4) Repeat the processes 1 and 2 until one of the first and second conditions is false.

 

After talking about this, how should I write the code? The following is an example I wrote. You are welcome to write your own ideas:

 

 

Int count_zero_number (int value)

{

Int count;

Int index;

Int * pData;

Int flag_two;

Int flag_five;

If (value <= 0)

Return 0;

 

PData = (int *) malloc (sizeof (int) * value );

Assert (NULL! = PData );

Memset (pData, 0, sizeof (int) * value );

 

For (index = 0; index <value; index ++ ){

PData [index] = index + 1;

}

 

Count = 0;

Do {

/* Reset the flag value */

Flag_two = 0;

Flag_five = 0;

 

For (index = 0; index <value; index ++ ){

If (0 = (pData [index] % 2 )){

PData [index]/= 2;

Flag_two = 1;

Break;

}

}

 

If (! Flag_two)

Break;

 

For (index = 0; index <value; index ++ ){

If (0 = (pData [index] % 5 )){

PData [index]/= 5;

Flag_five = 1;

Count ++;

Break;

}

}

 

} While (flag_five );

 

Free (pData );

Return count;

}

Int count_zero_number (int value)

{

Int count;

Int index;

Int * pData;

Int flag_two;

Int flag_five;

If (value <= 0)

Return 0;

 

PData = (int *) malloc (sizeof (int) * value );

Assert (NULL! = PData );

Memset (pData, 0, sizeof (int) * value );

 

For (index = 0; index <value; index ++ ){

PData [index] = index + 1;

}

 

Count = 0;

Do {

/* Reset the flag value */

Flag_two = 0;

Flag_five = 0;

 

For (index = 0; index <value; index ++ ){

If (0 = (pData [index] % 2 )){

PData [index]/= 2;

Flag_two = 1;

Break;

}

}

 

If (! Flag_two)

Break;

 

For (index = 0; index <value; index ++ ){

If (0 = (pData [index] % 5 )){

PData [index]/= 5;

Flag_five = 1;

Count ++;

Break;

}

}

 

} While (flag_five );

 

Free (pData );

Return count;

}

 

 

 

 

[Notice: The next blog will mainly introduce random poker algorithms]

Related Article

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.