UVA 568 (13.07.28)
Just the Facts |
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 lastnon-zero digit of anyfactorial for (). For example, if your program is asked tocompute the last nonzerodigit 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 not exceeding 10000,each on its own linewith no other letters, digits or spaces. For each integer N, you shouldread the value and compute the last nonzero digit of N!.
Output
For each integer input, the program should print exactly one line ofoutput. Each line of outputshould contain the value N, right-justified in columns 1 through 5 withleading blanks, not leadingzeroes. Columns 6 - 9 must contain `` -> " (space hyphen greater space).Column 10 must contain the single last non-zero digit of N!.
Sample Input
1
2
26
125
3125
9999
Sample Output
1 -> 1
2 -> 2
26 -> 4
125 -> 8
3125 -> 2
9999 -> 8
题意:
给出N, 求N的阶乘的最后一位是什么, 除零以外
思路:
考虑到零都是由2*5得来的, 那么每一次阶乘的时候, 把要乘的数对2和5去判断是否整除, 并统计整除2和5的次数分别是多少
最后我们再看看对2和5整除的次数分别是多少, 哪个多就还原哪个, 还原到对2对5的整除次数一致为止, 保证了2*5=10的成对性
AC代码如下:
#include<stdio.h> int main() {
int N;
int ans, t, num2, num5;
while(scanf("%d", &N) != EOF) {
ans = 1;
num2 = num5 = 0;
for(int i = N; i > 1; i--) {
t = i;
while(t % 2 == 0) {
num2++;
t = t / 2;
}
while(t % 5 == 0) {
num5++;
t = t / 5;
}
ans = ans * t;
ans = ans % 10;
}
if(num2 > num5) {
while(num2 > num5) {
ans = ans * 2;
ans = ans % 10;
num2--;
}
}
if(num5 > num2) {
while(num5 > num2) {
ans = ans * 5;
ans = ans % 10;
num5--;
}
}
printf("%5d -> %d\n", N, ans);
}
return 0;
}
UVA 568 (13.07.28)的更多相关文章
- UVA 10392 (13.07.28)
Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...
- UVA 408 (13.07.28)
Uniform Generator Computer simulations often require random numbers. One way to generatepseudo-ran ...
- UVA 299 (13.07.30)
Train Swapping At an old railway station, you may still encounter one of the lastremaining ``train ...
- UVA 140 (13.07.29)
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and anorderi ...
- 07-09 07:28:38.350: E/AndroidRuntime(1437): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.googleplay.ui.activity.MainActivity" on path: DexPathList[[zip file "/data/app/c
一运行,加载mainActivity就报错 布局文件乱写一通,然后急着运行,报莫名其妙的错误: 07-09 07:28:38.350: E/AndroidRuntime(1437): Caused b ...
- Feb 5 13:07:52 plugh rsyslogd-2177: imuxsock begins to drop messages from pid 12105 due to rate-limiting
FROM:https://www.nri-secure.co.jp/ncsirt/2013/0218.html SANSインターネットストームセンターのハンドラであるJohannes Ullrichが ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33
06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
随机推荐
- Maven学习笔记(四):协调和依赖
Maven协调具体的解释: Maven定义了这样一组规则:世界上不论什么一个构件都能够使用Maven坐标唯一标识.Maven坐标的元素包含groupId.artifactId.version ...
- kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据
kendo ui单击取消编辑数据buttongrid数据缩减.原因grid编辑数据追打datasorce于data寻找阵列数据的存在.假定有不会加入,加入缺席. 首先一个样本: html代码: < ...
- smb_精简安装
yum install samba vim /etc/samba/smb.conf [修改下自己要发布的目录 .eg : path = /home/iknow] smbpasswd -a ik ...
- 【高德地图API】从零开始学高德JS API(七)——定位方式大揭秘
原文:[高德地图API]从零开始学高德JS API(七)——定位方式大揭秘 摘要:关于定位,分为GPS定位和网络定位2种.GPS定位,精度较高,可达到10米,但室内不可用,且超级费电.网络定位,分为w ...
- Linux开发环境的搭建和使用——Linux本必备软件SSH
SSH 至 Secure Shell 缩写.由 IETF 网络工作组(Network Working Group)开发:SSH 以建立应用层和传输层安全协议中的基础上. SSH 是眼下较可靠,专为远程 ...
- easyui 小知识
默认为今天 $(document).ready(function () { $(function () { var curr_time = new Date(); ...
- linux cat
cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的. (1)其语法格式为:cut [-bn] [file] 或 cut ...
- PL/SQL联系oracle成功可以sql解决的办法是检查表的名称无法显示
有时,因为机器突然断电或其他原因PL/SQL它甚至不能在数据库表后显示.序列和其它元素.使用SQL Windows运行查询一般查询,登录或同样的现象再次. 我是不是可以解决因重复登录的猜测是,PLSQ ...
- 一张地图,告诉你NodeJS命令行调试器语句
NodeJS提供脚本调试. 进入node debug xx.js您可以进入调试模式. 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- 关于Office 中的墨迹功能(可作word电子签名)
原文 关于Office 中的墨迹功能 通过使用 Microsoft Office 2003 中的墨迹功能,可使用 Tablet PC 和 Tablet 笔将手写笔记插入到 Microsoft Offi ...