c語言寫一函數(shù)計算200!的值的后面有幾個0
1 回答

飲歌長嘯
TA貢獻1951條經(jīng)驗 獲得超3個贊
#include <stdio.h>
#include <stdlib.h>
//test函數(shù)返回任意正整數(shù)n的階乘的尾部的0的個數(shù)
int test(int n)
{
int i = 0;
int j = 0;
int nCount = 0;
if (n >= 5)
{
for(i = 5; i <= n; i += 5)
{
for (j = 5; i /j*j == i ; j *= 5)
{
nCount++;
}
}
}
return nCount;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 1;
printf("輸入一個正整數(shù):\n");
scanf("%d", &n);
printf("%d!尾部0的個數(shù)是:%d\n", n, test(n));
system("pause");
return 0;
}
我通過仔細分析問題,編制了一種算法,計算n!分解因子中5的個數(shù)即是n!尾部0的個數(shù),編了以上程序進行運算,得出200!的尾部有49個0,不知道對不對,樓主參考。
- 1 回答
- 0 關(guān)注
- 779 瀏覽
添加回答
舉報
0/150
提交
取消