求N个数阶乘末尾除0后的数值。

主要的难点在于要把这个N个数所含的2和5的队数去掉。

网上方法很多很好。 不多说

Last non-zero Digit in N!

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

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
 
Recommend
JGShining
 
#pragma comment(linker, "/STACK:102400000,102400000") 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff int n;
int save[]={,,,,,,,,,};
char s[];
int d[];
int i,ti;
int tmp1;
int cnt; int dfs(int len)
{
int tmp=;
for(i=len;i>=;i--)
if(d[i]!=) break;
if(i<)
{
return ;
}
if(i==)
{
return save[d[]];
}
if(d[]%==) tmp=;
else tmp=; tmp = ( tmp*save[d[]] )%; ti=i;
for(;i>=;i--)
{
tmp1=d[i]%;
d[i]/=;
if(i!=)
d[i-]+=tmp1*; //将余数向下推
} return (tmp*dfs(ti))%;
} int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","w",stdout);
while(~scanf("%s",s))
{
//主要是将所有数2和5因子提取出来就可以了,剩下来的取最后一个数即可
//然后就是最后一位怎么看了,
int len=strlen(s);
memset(d,,sizeof(d));
cnt=;
for(int i=;i<len;i++)
d[len--i]=s[i]-'';
printf("%d\n",dfs(len-));
}
return ;
}

2015.11.20.。。

又走到这步。 今天看数论的是看不小心瞄到了这题,心想多年前都能做出来,现在怎么没什么想法了。

然后推了半天,自己想出一个解法。

我们可以发现,每乘5个数(1-5,或6-10),相当于乘2.(然后再往5^2 ,5^3...推)

然后就很好做了,把n装化为5进制,然后一下就可以出结果了。

留个代码纪念下。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std; char str[];
int num[];
int ans[]; int chg(char c)
{
return c-'';
} int main()
{
while( scanf("%s",str) != EOF )
{
int len=strlen(str);
for(int i=;i<len;i++)
num[i] = chg( str[i] );
int cnt=;
for(int i=;i<len;i++)
if(num[i]==) cnt++;
else break;
int wei=;
int from=,to=;
while( cnt < len )
{
//然后做一次除法
for(int i=cnt;i<len;i++)
{
num[ i+ ] += (num[i]%to)*from;
num[ i ] /= to;
}
ans[ wei++ ] = num[len]/from;
num[len]=;
for(int i=cnt;i<len;i++)
{
if(num[i]==) cnt++;
else break;
}
}
/*
for(int i=wei-1;i>=0;i--)
{
printf("%d",ans[i]);
}
printf("\n");
*/
int tmp = ;
int sum = ;
for(int i=;i<wei;i++)
{
int some=ans[i+]%==?:;
for(int j=+some;j<=ans[i]+some;j++)
{
sum = (sum*j*tmp);
sum = sum%;
}
tmp = tmp*;
tmp = tmp%;
}
printf("%d\n",sum);
}
return ;
}

最新文章

  1. RedHat的定制安装
  2. 标准W3C盒子模型和IE盒子模型
  3. js类(继承)(一)
  4. Orchard特性路线图(其实就是以后将做什么)
  5. Linux瑞士军刀:密码管理Keeweb
  6. C#Winform从页面获取数据,传入数据库
  7. Rspec中describe和context不同
  8. delphi 调用 webservice (.NET C#版)
  9. android 9Path图片的使用
  10. mongodb内嵌文档的查询
  11. volatile解析(转)
  12. AT&amp;amp;T汇编——在你开始写
  13. 用元类和__getattribute__改变类属性的读取方式
  14. 关于微信JS-SDK 分享接口的两个报错记录
  15. Java基础(四)线程快速了解
  16. GUI_文件管理器(练习)
  17. 【个人总结】软件工程M1/M2总结
  18. bat脚本禁用和开启本地连接
  19. linux添加桌面快捷方式
  20. 系列文章--oracle简单入门教程

热门文章

  1. FIS3项目构建
  2. Python-绑定与未绑定方法通俗讲解
  3. vs2017安装过程问题及解决方法
  4. Js日常笔记之数组
  5. SQL中intersect、union、minus和except 运算符
  6. Eclipse - JAR包制作
  7. mongodb - mongotop
  8. 在Unity控制台下使用富文本
  9. ArcGIS教程:Geostatistical Analyst 应用演示样例
  10. Java遍历包中所有类