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)的更多相关文章

  1. UVA 10392 (13.07.28)

    Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...

  2. UVA 408 (13.07.28)

     Uniform Generator  Computer simulations often require random numbers. One way to generatepseudo-ran ...

  3. UVA 299 (13.07.30)

     Train Swapping  At an old railway station, you may still encounter one of the lastremaining ``train ...

  4. 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 ...

  5. 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 ...

  6. 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が ...

  7. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  8. 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 ...

  9. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

随机推荐

  1. DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表

    原文:DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助 ...

  2. NSString 筛选和最后一个空白、空行,多换行成一个新行

    - (NSString *)filterBlankAndBlankLines:(NSString *)str { NSMutableString *Mstr = [NSMutableString st ...

  3. sql server判断表是否存在

    1.判断数据表是否存在 方法一: use yourdb;go if object_id(N'tablename',N'U') is not nullprint '存在'else print '不存在' ...

  4. zoj 3210 A Stack or A Queue? (数据结构水题)

     A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? ...

  5. 数学思想方法-分布式计算-linux/unix技术基础(3)

    夹: ~表示当前用户的主文件夹 .它代表了当前文件夹 ..它代表的父文件夹 链接文件 使用不同的文件名指的是相同的数据或程序.硬链接 在相同的物理文件系统,创建一个硬链接 -bash-4.2$ fin ...

  6. Ubuntu14.04安装一个小问题,搜狗输入法

    罕见的搜狗输入法支持ubuntu.尝试了决定性下载. 官方网站:http://pinyin.sogou.com/linux/ 官网教程:http://pinyin.sogou.com/linux/he ...

  7. 2440裸 Delay(); 和 while(!(rUTRSTAT0 &amp; 0x2)); 问题

    前两天写RTC中断 使用串行输出 它发现,该方案将while(!(rUTRSTAT0 & 0x2));走不出的情况.的 解决方法: main函数添加: U32 mpll_val = 0,con ...

  8. Swift学习笔记(一)搭配环境以及代码运行成功

    原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...

  9. MySQL编程(0) - Mysql中文乱码问题解决方案

    MySQL 5.6 for Windows 解压缩版配置安装: http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html MySQL ...

  10. 详细的图文介绍如何利用XAMPP本地建站的环境配置教程

    原文:详细的图文介绍如何利用XAMPP本地建站的环境配置教程 WordPress 是一个简便快捷,用途广,人气旺的一个开源的博客建站程序.很有很多等您去发现. 简便快捷:在性能上易于操作.易于浏览: ...