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));
代码如下.
 #include<stdio.h>
int main()
{
int n,i;
_int64 ans;
while(scanf("%d",&n)!=EOF)
{
ans=;
for(i=;i<=n;i++)
{
ans*=i;
while((ans%)==) ans/=;
ans%=;
}
while((ans%)==) ans/=;
ans%=;
printf("%I64d\n",ans);
}
return ;
}

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

代码:

 #include<stdio.h>
#include<string.h>
#define maxn 1000
const int mod[]={,,,,,,,,,,,,,,,,,,,};
char str[maxn];
int a[maxn];
int main()
{
int len,i,c,ret;
while(scanf("%s",str)!=EOF)
{
len=strlen(str);
ret=;
if(len==) printf("%d\n",mod[str[]-'']);
else
{
for(i=;i<len;i++)
a[i]=str[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%=;
}
}
printf("%d\n",ret+ret%*);
}
}
return ;
}

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. mac securecrt无法记住密码的解决方法

    打开secureCRT,菜单preferences--general,找到mac options.然后去掉Use KeyChain选项,这样每次连接服务器后就会自动保存密码了.不同的版本可能这个选项的 ...

  2. [11] 楔形体(Wedge)图形的生成算法

    顶点数据的生成 bool YfBuildWedgeVertices ( Yreal width, Yreal length, Yreal height, YeOriginPose originPose ...

  3. 第六章 memcached剖析

    注:本篇博客参考于两本书. <memcached全面剖析>,该书籍市面上应该没有,我传到了百度云盘,链接如下:http://pan.baidu.com/s/1qX00Lti <大型网 ...

  4. Pow(x,n) leetcode java

    题目: Implement pow(x, n). 题解: pow(x,n)就是求x的n次方.x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2).所以利用递归求解,当n==1 ...

  5. smtp发送带附件的邮件(直接将string类型结果保存为附件)

    该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好    MailMessage mmsg = new MailMessage();    mmsg.Subject = " ...

  6. 浅谈jQuery easyui datagrid操作单元格样式

    今天项目上遇到问题,就是表格风格统一的问题,由于用了2个不同的框架,所以如果要大修比较麻烦,考虑到修改表格样式工作量会少很多,所以考虑修改jQuery easyui datagrid数据网格的样式. ...

  7. CSS深入了解border:利用border画三角形等图形

    三角形实际上是border的产物 我们正常使用的border都是四边一个颜色,当我们把四边换上不同颜色 那么你就会发现,三角来了~! <!DOCTYPE html> <html la ...

  8. 协定须要双工,可是绑定“WSHttpBinding”不支持它或者因配置不对而无法支持它

    协定须要双工,可是绑定"WSHttpBinding"不支持它或者因配置不对而无法支持它 下面两种情况,我都遇到过. 一, < endpoint address =" ...

  9. WordPress 后台评论如何自定义搜索条件

    大家都知道WordPress 作为一个非常成熟的博客系统,功能可以说是非常强大,几乎整个网站都可以进行定制开发,已经不算是一个博客系统了而应该是一个成熟的开发框架 最近就用WP给客户开发了一个网站,但 ...

  10. ElasticSearch关闭重启命令

    很多人学习elasticSearch都是自学,想百度一下如何重启es也是没有答案,我硬着头皮,算是琢磨出来了,借此写博,希望能帮助您. 1.如何关闭ES,elasticsearch关闭办法 1.使用h ...