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)