Algorithm - n的阶乘末尾0的个数

来源:互联网 发布:儿童网络教学 编辑:程序博客网 时间:2024/04/27 21:18

1. 示例程序1:

int lowest_one(unsigned n){    int count = 0;    unsigned int i, j;    /* 对n!进行质因子5的分解,计算能分解得到5的多少次方,即为所求 */    for (i = 1; i <= n; i++)    {        j = i;        while (0 == j % 5)        {            count++;            j /= 5;        }    } // end of for    return count;} // end

2. 示例程序2:

int lowest_one_other(unsigned n){    int count = 0;    /* 5的倍数的数值依次贡献质因子5 */    while (n)    {        count += n / 5;        n /= 5;    } // end of while    return count;} //end


原创粉丝点击