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. find your present (2) 2095

    Problem Description In the new year party, everybody will get a "special present".Now it's ...

  2. C#二维码生成

    C#二维码生成,这里使用开源的ThoughtWorks.QRCode.dll库. 1.下载ThoughtWorks.QRCode.dll库文件,并引用到项目中. 2.创建QRCodeHandler.c ...

  3. HDU 2070 Fibbonacci Number

    Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. C++ 对象模型具体评论(特别easy理解力)

    c++对象模型系列 转 一.指针与引用 一 概括 指针和引用,在C++的软件开发中非经常见,假设能恰当的使用它们可以极大的提 高整个软件的效率,可是非常多的C++学习者对它们的各种使用情况并非都了解, ...

  5. 【百度地图API】如何制作一张魔兽地图!!——CS地图也可以,哈哈哈

    原文:[百度地图API]如何制作一张魔兽地图!!--CS地图也可以,哈哈哈 摘要: 你玩魔兽不?你知道如何做一张魔兽地图不?! 快来看此文吧! ---------------------------- ...

  6. 内网穿透&UDP打洞

    这两天找度度重新回忆了一下关于内网穿透的事情,在百度文库上找到了两三篇写的比较通俗易懂的文章,把内网穿透做个简单总结. 首先文章建议 Cone NAPT 还有希望,要是 Symmetri NAPT 就 ...

  7. iOS开展 - 中国 iOS/Mac 开发博客列表

    博客地址 RSS地址 OneV's Den http://onevcat.com/atom.xml 破船之家 http://beyondvincent.com/atom.xml NSHipster h ...

  8. ActionScript GifPlayer的修改

    ActionScript不能播放gif格式的图片,在做as项目的时候如果需要用到加载gif动画图片时,就需要引入第三方包. 常用的第三方包是GifPlayer,在github上可以找到该项目的源代码C ...

  9. Jquery.validate表单验证

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/API: http://jquery.bassista ...

  10. .net使用cefsharp开源库开发chrome

    .net使用cefsharp开源库开发chrome 离上篇写介绍pc端的混合开发和为什么以cefsharp入手研究混合开发已经有好几天,一直忙,抽不出时间继续写怎么搭建cefsharp开发环境.其实没 ...