文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>N!的尾部连续0的个数

N!的尾部连续0的个数

时间:2009-08-07  来源:ubuntuer

对任意输入的正整数N,编写C程序求N!的尾部连续0的个数,并指出计算复杂度。如:18!=6402373705728000,尾部连续0的个数是3。 (不用考虑数值超出计算机整数界限的问题)   给出18!,以及括号里的不考虑越界,明显是用来误导大家直接求出!后/10   其实2的个数是足够多的,这个题目就引申为是求5的个数 5 10 15这样有三个5,注意先25是两个5.这样就可以了 代码思路都是很easy的  
 

#include <stdio.h>
#include <stdlib.h>

int count0(int N)
{
  int i;
  int j;
  int count = 0;
  
  for(i=5;i<=N;i+=5)
   {
     j = i;
     while(j>0 && j%5==0)
      {
        j = j/5;
        count++;
      }
   }
   return count;
}

int main(int argc, char *argv[])
{
  int N;
  printf("Please input the number you want to count:\n");
  scanf("%d",&N);
  printf("%d! has %d zeros\n", N, count0(N));
  system("PAUSE");    
  return 0;
}

相关阅读 更多 +
排行榜 更多 +
进击的小动物安卓版

进击的小动物安卓版

飞行射击 下载
进攻yalghaar

进攻yalghaar

飞行射击 下载
深空战场

深空战场

飞行射击 下载