HDUOJ-----1066Last non-zero Digit in N!
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=1;
9 for(i=2;i<=n;i++)
10 {
11 ans*=i;
12 while((ans%10)==0) ans/=10;
13 ans%=100000000;
14 }
15 while((ans%10)==0) ans/=10;
16 ans%=10;
17 printf("%I64dn",ans);
18 }
19 return 0;
20 }
代码精简,但是复杂度为O(n)。。。提交的时候果断的tle了,爱,好忧伤呀~~~!,后来想了想,能否将其优化勒!
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #define maxn 1000
4 const int mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};
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=1;
14 if(len==1) printf("%dn",mod[str[0]-'0']);
15 else
16 {
17 for(i=0;i<len;i++)
18 a[i]=str[len-1-i]-'0'; //将其转化为数字以大数的形式
19 for( ; len>0; len-=!a[len-1])
20 {
21 ret=ret*mod[a[1]%2*10+a[0]]%5;
22 for(c=0, i=len-1 ;i>=0;i--)
23 {
24 c=c*10+a[i];
25 a[i]=c/5;
26 c%=5;
27 }
28 }
29 printf("%dn",ret+ret%2*5);
30 }
31 }
32 return 0;
33 }
- 使用JClouds在Java中获取和发布云服务器
- 利用ASP.NET SiteMap生成与Bootstrap"兼容"菜单
- 埃隆·马斯克强烈推荐的5本书,看完之后他开始改变世界
- 算法:AOE网(Activity On edge Network)与关键路径简介
- ASP.NET Core的配置(4):多样性的配置来源[中篇]
- ASP.NET MVC的Razor引擎:RazorViewEngine
- 算法:求解AOE网的关键路径
- 编程小技巧:多态原理
- ASP.NET Core的配置(4):多样性的配置来源[下篇]
- 数据结构 图
- ASP.NET Core的配置(5):配置的同步[ 实例篇]
- TensorFlow 深度学习笔记 卷积神经网络
- 利用EntLib授权机制实现对ASP.NET页面的自动授权
- ASP.NET Core的配置(5):配置的同步[设计篇]
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法