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. Javascript学习2 - Javascript中的表达式和运算符

    原文:Javascript学习2 - Javascript中的表达式和运算符 Javascript中的运算符与C/C++中的运算符相似,但有几处不同的地方,相对于C/C++,也增加了几个不同的运算符, ...

  2. Flex列在一个表格式的数字值

    1.问题背景 一般的.表格中展示的比率.对照率的处理是:保留两位小数,并向上保留 2.实现实例 <? xml version="1.0" encoding="utf ...

  3. Yeoman generators 创建 ASP.NET 应用程序

    Yeoman generators 创建 ASP.NET 应用程序 上一篇:<Windows 搭建 .NET 跨平台环境并运行应用程序> 阅读目录: Install Node.js Ins ...

  4. POJ 1742 Coins (多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Descriptio ...

  5. Asp.Net MVC 2.0 Filter基本用法

    在这一节里,大家一同学习下mvc 2.0中的filter,简单的说,filter就是标记在action上的一些属性,来实现对action的控制. mvc2.0中主要包括以下filter 1. Auth ...

  6. NYOJ 105 其余9个

    九的余数 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 如今给你一个自然数n,它的位数小于等于一百万,如今你要做的就是求出这个数整除九之后的余数. 输入 第一行有一 ...

  7. 远程数据client交换器

    不太繁忙的文本. 要被写入

  8. crawler_Docker_解决用 JavaScript 框架开发的 Web 站点抓取

    [转载,后续补上实践case] 有了 Docker,用 JavaScript 框架开发的 Web 站点也能很好地支持网络爬虫的内容抓取 [编者的话]Prerender 服务能够为网络爬虫提供预先渲染的 ...

  9. Web文件(图片)上传方法

    在开放Web应用程序的时候经常会遇到图片或者是文件上传的模块,这里就是该模块的实现的后台方法 上传图片方法 /// <summary> /// 功能:上传图片方法 /// </sum ...

  10. DevExpress的Web控件汉化方法

    原文:DevExpress的Web控件汉化方法 项目中用到devexpress的web控件,机器没有安装devexpress控件,直接在项目中引用的dev的dll,项目运行时发现都是英文界面,所以解决 ...