Last non-zero Digit in N!

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

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
经过细致的观察,发现n!的阶乘,要求其最后一位非0,便是要去掉所有的0 ...比如
6!=720..
我们在循环的时候,只需要取其长度取摸就可以了,ans%strlen(itoa(6));
代码如下.
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n,i;
  5. _int64 ans;
  6. while(scanf("%d",&n)!=EOF)
  7. {
  8. ans=;
  9. for(i=;i<=n;i++)
  10. {
  11. ans*=i;
  12. while((ans%)==) ans/=;
  13. ans%=;
  14. }
  15. while((ans%)==) ans/=;
  16. ans%=;
  17. printf("%I64d\n",ans);
  18. }
  19. return ;
  20. }

代码精简,但是复杂度为O(n)。。。提交的时候果断的tle了,爱,好忧伤呀~~~!,后来想了想,能否将其优化勒!

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define maxn 1000
  4. const int mod[]={,,,,,,,,,,,,,,,,,,,};
  5. char str[maxn];
  6. int a[maxn];
  7. int main()
  8. {
  9. int len,i,c,ret;
  10. while(scanf("%s",str)!=EOF)
  11. {
  12. len=strlen(str);
  13. ret=;
  14. if(len==) printf("%d\n",mod[str[]-'']);
  15. else
  16. {
  17. for(i=;i<len;i++)
  18. a[i]=str[len--i]-''; //将其转化为数字以大数的形式
  19. for( ; len>; len-=!a[len-])
  20. {
  21. ret=ret*mod[a[]%*+a[]]%;
  22. for(c=, i=len- ;i>=;i--)
  23. {
  24. c=c*+a[i];
  25. a[i]=c/;
  26. c%=;
  27. }
  28. }
  29. printf("%d\n",ret+ret%*);
  30. }
  31. }
  32. return ;
  33. }

HDUOJ-----1066Last non-zero Digit in N!的更多相关文章

  1. [LeetCode] Nth Digit 第N位

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...

  2. [LeetCode] Number of Digit One 数字1的个数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  3. [Leetcode] Number of Digit Ones

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  4. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  5. kaggle实战记录 =>Digit Recognizer

    date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...

  6. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  7. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  8. Last non-zero Digit in N!(阶乘最后非0位)

    Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. POJ3187Backward Digit Sums[杨辉三角]

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6350   Accepted: 36 ...

  10. Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

随机推荐

  1. uestc 360(区间合并)

    题意:有一个长度为n的序列.然后有两种操作,Q a b是输出区间a b内最长上升子序列的长度.A a b c是把区间a b内全部数字加上c. 题解:用线段树维护区间的最长上升子序列长度,那么一个区间的 ...

  2. 基于jQuery的Cookie操作插件--简单而又没有兼容性问题!

    在网页客户端,我们经常会遇到读取或者设置cookie的情况,如果用纯生的js我们可能会遇到一些兼容性带来的麻烦,这里给大家介绍一个比较实用jquery操作cookie的插件,插件的源代码如下: 1 2 ...

  3. vue路由使用踩坑点:当动态路由再使用路由name去匹配跳转时总是跳转到根路由的问题

    闲话少说,直接问题: 之前我的路由时这么写的 { path:'/serverInfo/:id', name:'serverInfo', component:() => import('@/vie ...

  4. 关于一道JS面试题的思考

    题目: ; i < ; i++) { setTimeout(function() { console.log(new Date, i); }, ); } console.log(new Date ...

  5. 理解JavaScript私有作用域

    私有作用域:跟外界的变量方法毫不冲突,豪无关系 var str ="javascript"; (function(){ alert(str); //undefined var st ...

  6. android 中的 window,view,activity具体关系

    通过讨论这个问题,我们能够见识到google是对面向对象模式的理解,能够理解android底层的一些调用.这也是一道很常见的面试题. 我们这篇文章就来解决这四个问题: Android  中view的显 ...

  7. 【ES】elasticsearch学习笔记

    ES学习 1 优势 1.1 简单 1.1.1 相比Solor配置部署等非常简单 1.2 高效 1.2.1 ES使用Netty作为内部RPC框架,Solor使用Jetty 1.3 插件化 1.3.1 E ...

  8. WPF DataTrigger的两个用法

    1.用在textbox等输入控件上,验证输入是否合法.首先定义一个Converter, 2.Xaml里面这样子写,意思是输入的数字,如果小于100则显示为红色. 3.combox等列表控件里面,也可以 ...

  9. ECMAScript5之Object学习笔记(二)

    继续第二部分 Object.freeze(obj) 看字面意思就是“把一个对象冻结”. 下面我们来看个简单的例子以作说明: // a person instance var person = { na ...

  10. 微信小程序中用户唯一ID的获取

    折腾到半夜,搞得挺兴奋,总结一下,免得忘了: 1.微信小程序直接获得的是一些简单信息,基本无用 2.用户唯一标识是openid,还有一个unionid是关联多个公众号之类情况下用,我不大关心 3.在g ...