[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;}
[Notice: The next blog will mainly introduce random poker algorithms]