求阶乘集合

 

计算10000以内的阶乘

/* Factorial.c  -- 计算大数的阶乘

  * Author: Space

  * Date:    2007/07/03

  * Version: 1.0

  */

#include<stdio.h>

#include<stdlib.h>  // for malloc()

#include<string.h>  // for memset()

#define QUOTIETY  4  

// 内存分配系数,计算10000以内阶乘设置为4就足够,如果需要

// 计算更大的数的阶乘,则将该系数适当增大

void process(const int index, int *result);

int cnt = 1;

int main(void)

{

int index = 0;

int input = 0;

int *result = NULL;

// 获得输入数据

printf("请输入你要计算的阶乘数,内存有限,请不要超过10000:\n");

scanf("%d", &input);

while (input <= 0 || input > 10000)

{

printf("请输入合理的数据,谢谢:\n");

scanf("%d", &input);

}

// 申请空间储存计算结果

result = (int *)malloc(sizeof(int) * input * QUOTIETY);

if (result == NULL)

{

printf("内存申请失败!\n");

exit(-1);

}

memset(result, 0, sizeof(int) * input * QUOTIETY);  // 初始化存储空间

result[0] = 1;

// 进行阶乘计算

for ( index = 1; index <= input; ++index)

{

process(index, result);

}

// 打印结果

for (index = cnt - 1; index >= 0L; --index)

{

printf("%d", result[index]);

}

putchar('\n');

printf("结果一共有%d位数!\n", cnt);

free(result);

system("pause");

return 0;

}

/*

*  计算阶乘核心代码

  */

void process(const int index, int *result)

{

int product = 0;  // 乘积

int carry = 0;    // 进位

int remainder = 0;  // 余数

int i = 0;

for (i = 0; i < cnt; ++i)

{

product = result[i] * index + carry;

carry = product / 10;

remainder = product % 10;

result[i] = remainder;

}

if (carry != 0)

{

while (carry / 10 != 0)

{

result[cnt] = carry % 10;

carry /= 10;

++cnt;

}

result[cnt++] = carry;

}

}

 

有一些又是求一个数N!中末尾有多少个0?

这是我的思路

int count(int n)
{
   int count=0;//计数器
    int total =1;//乘积结果
     for(int i =1; i<=n;i++)   
    {      total = i*total;
         if(total%10==0)
         {
             count++;
             total=total/10;           
         }   
    
    }
    return count;
}
另一个算法:
就是寻找整除5的数
int First()
{
   int ret = 0;
   int j=0;
   for(int i=1; i<=N;i++)
   {
        j=i;
        while(j%5==0)
        {
           ret++;
           j=j/5;
        }   
           
    }   
    return ret;
}

int Second(int N)
     int ret=0;
     while(N)
     {
        ret=ret+N/5;
        N=N/5; 
             
     }
    return ret;
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值