[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]