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
 
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #define MAXN 10000
  6.  
  7. int lastdigit(char* buf){
  8. const int mod[]={,,,,,,,,,,,,,,,,,,,};
  9. int len=strlen(buf),a[MAXN],i,c,ret=;
  10. if (len==)
  11. return mod[buf[]-''];
  12. for (i=;i<len;i++)
  13. a[i]=buf[len--i]-'';
  14. for (;len;len-=!a[len-]){
  15. ret=ret*mod[a[]%*+a[]]%;
  16. for (c=,i=len-;i>=;i--)
  17. c=c*+a[i],a[i]=c/,c%=;
  18. }
  19. return ret+ret%*;
  20. }
  21. int main()
  22. {
  23. char a[]="\0";
  24. while(scanf("%s",a)!=EOF)
  25. {
  26. int ans=lastdigit(a);
  27. printf("%d\n",ans);
  28. }
  29. return ;
  30. }

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. sql server 查询和Kill死锁进程

    查询死锁进程语句 select        request_session_id spid,       OBJECT_NAME(resource_associated_entity_id) tab ...

  2. UIWebView显示乱码问题

    今天有人问Swift中UIWebView加载页面出来的是乱码,问知道怎么解决么? OC我知道肯定不会有乱码问题,Swift就不知道了,因为没有试过.....于是...我自己动手试试啦... OC和Sw ...

  3. 利用Unity制作“表”

    一枚小菜鸟   目前没发现在Unity有其他路径制作类似于c# WinForm中的表:但是利用Unity自带的UGUI,制作了一张"伪表",具体方案如下: 效果图如下: 步骤: 1 ...

  4. Android 7.0 UICC 分析(一)

    UICC(Universal Intergrated Circuit Card) 框架 * Following is class diagram for uicc classes: * * UiccC ...

  5. 第四十四章 微服务CICD(6)- gitlab + jenkins + docker + k8s

    总体流程: 在开发机开发代码后提交到gitlab 之后通过webhook插件触发jenkins进行构建,jenkins将代码打成docker镜像,push到docker-registry 之后将在k8 ...

  6. UiAutomator自动化测试框架介绍

    UiAutomator自动化测试框架介绍 环境搭建 1         必要条件 1.1       JDK 1.2       SDK(API高于15) 1.3       Eclipse 2    ...

  7. 使用selenium控制滚动条(非整屏body)

    方法原理:     (1)使用jQuery CSS 操作 - scrollTop() 方法,设置 <div> 元素中滚动条的垂直偏移,语法:$(selector).scrollTop(of ...

  8. Window 命令

    tracert XXX.XXX.XXX.XXX 路由追踪命令,可以显示到目的IP所经过的路由    

  9. mount: unknown filesystem type 'ntfs'

    mount: unknown filesystem type 'ntfs' 问题描述 # mount –t ntfs /dev/sdc1 /mnt/usb2 mount: unknown filesy ...

  10. Tp field 字段是可以添加函数的

    $info = M('Order')->alias('a') ->field('count(DISTINCT(a.order_user_id)) as buy_user_num,count ...