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. MySQL root密码找回

    以MySQL多实例为例,演示找回MySQL root的密码 1.关闭mysql服务 [root@mysql ~]# mysqladmin -uroot -poldboy123 -S /data/330 ...

  2. StaticPagedList

    估计是因为水平原因,之前看别人写的用pagedList分页,老是云里雾里的.下面把自己写的整理一下放在上面.这里的List为对应页面展示的内容.不用查询所有. Action: public Actio ...

  3. hdu4578 Transformation

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 题目大意:n个数(初始为0)m个操作,操作类型有4种,操作1把区间的每个数+a,操作2把区间的每 ...

  4. PTA Iterative Mergesort

    How would you implement mergesort without using recursion? The idea of iterative mergesort is to sta ...

  5. ANDROID开发之问题积累及解决方案(一)

    一.activity跳转及传值 当进行activity之间的跳转时我们会遇到这样的问题.首先熟悉下activity之间跳转.Activity跳转与传值,主要是通过Intent类来连接多个Activit ...

  6. 自适应UITableView的Cell高度问题

    1.自己计算Cell的高度返回: 1>model中计算: // // InfoModel.h // OCDemo // // Created by 思 彭 on 16/12/27. // Cop ...

  7. UVM的类库

    [转]http://www.asicdv.com/ 一个UVM验证平台可以看成由多个模块组合在一起的,这和以前的verilog代码,以及verilog结合其它各种语言的验证手段在理念上是一样的,最大的 ...

  8. 又一种XML的解析方法

    [Fact(DisplayName="用户名为空")] public void Should_UsernameEmpty() { var paras = new Dictionar ...

  9. css总结

    1. css中的role属性 html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组 ...

  10. SimpleDateFormat的应用

    import java.text.SimpleDateFormat;import java.util.Date;public class Main {    public static void ma ...