Last non-zero Digit in N!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5861    Accepted Submission(s): 1451

Problem Description
The expression N!, read as "N factorial," denotes the product of the first N positive integers, where N is nonnegative. So, for example, N N! 0 1 1 1 2 2 3 6 4 24 5 120 10 3628800
For this problem, you are to write a program that can compute the last non-zero digit of the factorial for N. For example, if your program is asked to compute the last nonzero digit of 5!, your program should produce "2" because 5! = 120, and 2 is the last nonzero digit of 120.
 
Input
Input to the program is a series of nonnegative integers, each on its own line with no other letters, digits or spaces. For each integer N, you should read the value and compute the last nonzero digit of N!.
 
Output
For each integer input, the program should print exactly one line of output containing the single last non-zero digit of N!.
 
Sample Input
1

2

26

125

3125

9999
 
Sample Output
1

2

4

8

2

8
 
Source
 
 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXN 10000 int lastdigit(char* buf){
const int mod[]={,,,,,,,,,,,,,,,,,,,};
int len=strlen(buf),a[MAXN],i,c,ret=;
if (len==)
return mod[buf[]-''];
for (i=;i<len;i++)
a[i]=buf[len--i]-'';
for (;len;len-=!a[len-]){
ret=ret*mod[a[]%*+a[]]%;
for (c=,i=len-;i>=;i--)
c=c*+a[i],a[i]=c/,c%=;
}
return ret+ret%*;
}
int main()
{
char a[]="\0";
while(scanf("%s",a)!=EOF)
{
int ans=lastdigit(a);
printf("%d\n",ans);
}
return ;
}

Last non-zero Digit in N!(阶乘最后非0位)的更多相关文章

  1. NYOJ1026 阶乘末尾非0 【模板】

    阶乘末尾非0 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 我们的问题非常是简单.n! 末尾非0数是几? 比方n=5的时候,n! =120,那么n!末尾非0数是2. ...

  2. 计算阶乘n!末尾0的个数

    一.问题描述 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数.例如: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= ...

  3. N阶乘尾部的0个数

    N阶乘尾部的0个数 描述 设计一个算法,计算出n阶乘中尾部零的个数 思路: 1.1 * 2 * 3 * ... * n --> 1 * 2 * 3 * (2 * 2) * 5 * (2 * 3) ...

  4. Rightmost Digit (求n^n最后一位)

    Description Given a positive integer N, you should output the most right digit of N^N.    Input The ...

  5. POJ 1401:Factorial 求一个数阶乘的末尾0的个数

    Factorial Time Limit: 1500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 9349 Descri ...

  6. HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. 2018年东北农业大学春季校赛 E 阶乘后的0【数论】

    链接:https://www.nowcoder.com/acm/contest/93/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  8. 笔试算法题(33):烙饼排序问题 & N!阶乘十进制末尾0的个数二进制最低1的位置

    出题:不同大小烙饼的排序问题:对于N块大小不一的烙饼,上下累在一起,由于一只手托着所有的饼,所以仅有一只手可以翻转饼(假设手足够大可以翻转任意块数的 饼),规定所有的大饼都出现在小饼的下面则说明已经排 ...

  9. acm算法模板(1)

    1. 几何 4 1.1 注意 4 1.2 几何公式 4 1.3 多边形 6 1.4 多边形切割 9 1.5 浮点函数 10 1.6 面积 15 1.7 球面 16 1.8 三角形 17 1.9 三维几 ...

随机推荐

  1. Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号

    www.cnblogs.com/zhangzhu/archive/2013/08/22/3274831.html 如何开启MySQL的远程帐号-1)首先以 root 帐户登陆 MySQL 在 Wind ...

  2. RTTI 运行时类型识别 及异常处理

    RTTI   运行时类型识别 typeid  ------  dynamic_cast dynamic_cast 注意事项: 1.只能应用于指针和引用之间的转化 2.要转换的类型中必须包含虚函数 3. ...

  3. The user specified as a definer ('root'@'%') does not exist

    The user specified as a definer ('root'@'%') does not exist 此种报错主要是针对访问视图文件引起的(没有权限) 解决方法: 2.进入mysql ...

  4. java中的 FileWriter类 和 FileReader类的一些基本用法

    1,FileWriter类(字符输出流类) |--用来写入字符文件的便捷类.此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的.要自己指定这些值,可以先在 FileOutputStream ...

  5. 未能加载文件或程序集EntityFramework, Version=6.0.0.0解决办法

    其他信息: 未能加载文件或程序集"EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934 ...

  6. 利用GCTA工具计算复杂性状/特征(Complex Trait)的遗传相关性(genetic correlation)

    如文章"Genome-wide Complex Trait Analysis(GCTA)-全基因组复杂性状分析"中介绍的GCTA,是一款基于全基因组关联分析发展的分析工具,除了计算 ...

  7. 网页链接qq

    <a href="mqqwpa://im/chat?chat_type=wpa&uin=12345678&version=1&src_type=web& ...

  8. c语言冒泡排序

    在C语言中,常用的排序算法有:冒泡排序.快速排序.插入排序.选择排序.希尔排序.堆排序以及归并排序等等. 冒泡排序基本概念:  依次比较相邻的两个数,将小数放在前面,大数放在后面. #include ...

  9. dockManager中DockPanel的刷新问题!

    使用的是DevExpress的dockManager控件,新增一个DockPanel,在DockPanel中添加一个AxMapControl控件并设置默认加载地图. 实现效果图如下: 但是存在一个问题 ...

  10. AFN 无网络监控

    直接把封装到请求里就好 -(void)afn{ //1.创建网络状态监测管理者 AFNetworkReachabilityManager *manger = [AFNetworkReachabilit ...