Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4746208
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-08-07 10:01:32

对任意输入的正整数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;
}

阅读(1562) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~