Step-by-step write algorithm (n! Count of the number of zeros in the end)

Source: Internet
Author: User

Text: One-step-one-step writing algorithm (n! Count of the number of zeros in the end)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


In many interview questions, ask for n! The number of zeros in the results is also a frequently encountered topic. So what is the solution to this problem? I would like to share with you some of my own views, with different views of friends welcome more comments.

Beg n! The number of zeros is mainly in the multiplier is not divisible by 2 and 5 number, as long as you can find the multiplier of 2 and 5 integers, so, my code flow is this:

(1) Find out if there are no integers in the current data that can be divisible by 2, and change the numeric value of integers

(2) Find out if there are no integers in the current data that can be divisible by 5, and change the numeric value of integers

(3) If the conditions of 1 and 2 are met at the same time, it means that there is a zero, count++

(4) Repeat the process of 1, 2, until 1, 2 has a condition of false

Having said so much, how should the code be written? Here is an example of my personal writing, and 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 = = (pDa Ta[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;}


"Preview: Next Blog main introduction random poker Algorithm"



Step-by-step write algorithm (n! Count of the number of zeros in the end)

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.