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. 1
  2. 2
  3. 26
  4. 125
  5. 3125
  6. 9999

Sample Output

  1. 1 -> 1
  2. 2 -> 2
  3. 26 -> 4
  4. 125 -> 8
  5. 3125 -> 2
  6. 9999 -> 8

题意:

给出N, 求N的阶乘的最后一位是什么, 除零以外

思路:

考虑到零都是由2*5得来的, 那么每一次阶乘的时候, 把要乘的数对2和5去判断是否整除, 并统计整除2和5的次数分别是多少

最后我们再看看对2和5整除的次数分别是多少, 哪个多就还原哪个, 还原到对2对5的整除次数一致为止, 保证了2*5=10的成对性

AC代码如下:

  1. #include<stdio.h>
  2.  
  3. int main() {
  4. int N;
  5. int ans, t, num2, num5;
  6. while(scanf("%d", &N) != EOF) {
  7. ans = 1;
  8. num2 = num5 = 0;
  9. for(int i = N; i > 1; i--) {
  10. t = i;
  11. while(t % 2 == 0) {
  12. num2++;
  13. t = t / 2;
  14. }
  15. while(t % 5 == 0) {
  16. num5++;
  17. t = t / 5;
  18. }
  19. ans = ans * t;
  20. ans = ans % 10;
  21. }
  22. if(num2 > num5) {
  23. while(num2 > num5) {
  24. ans = ans * 2;
  25. ans = ans % 10;
  26. num2--;
  27. }
  28. }
  29. if(num5 > num2) {
  30. while(num5 > num2) {
  31. ans = ans * 5;
  32. ans = ans % 10;
  33. num5--;
  34. }
  35. }
  36. printf("%5d -> %d\n", N, ans);
  37. }
  38. return 0;
  39. }

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的小结 前两种方法和WinForm一样,可以传递参数.数组.实体对象.DataTable等1. 采用构造函数具体用法:在Report中p ...

  2. js中位运算的运用

    原文:js中位运算的运用 我们可能很少在编程中用位运算,如果没深入学习,可能也很难理解.平时的数值运算,其实是要先转换成二进制再进行运算的,而位运算就是直接进行二进制运算,所以位运算的执行效率肯定是更 ...

  3. 深入了解setInterval方法

    相信大家对setInterval方法肯定非常熟悉,但不少人对其缺乏深入的了解,致使当一个flash里有多个setInterval的时候就容易混淆,该清除的间隔lID没有清除,不该清除的时候却清除了.对 ...

  4. swiftSingleton模式

    swift在几个方面Singleton模式: 1. 全局变量 private let _singleton = Singleton() class Singleton: NSObject { clas ...

  5. set RowCount 与 top n

    有时,采用top n中间n它是一个变量,这将需要使用()去完成: declare @count1 int set @count1 = 8 select top <strong>(@coun ...

  6. Linux常用命令2--用户问题、文件的打包压缩

    Linux常用命令 如何进行用户和群组的创建和更改 [1]groupadd:用于创建新的群组. 语法:groupadd [-option] 用户名:其常用参数有:-g groupadd -g 555 ...

  7. 安装 CocoaPods & Alcatraz

    (一)安装CocoaPods { CocoaPods :} 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使 ...

  8. SVN & Git (一)

    (一)SVN的使用.CornerStone图形化管理工具! SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS. ...

  9. Java-继承特性

    继承的特点: 1.提高了代码的复用性. 2.让类与类之间发生了关系,有了这个关系,才有了多态的特性. (注意:千万不要为了获取其他类的功能,简化代码而继承:必须是类与类之间有所属关系才可以继承,所属关 ...

  10. 如何使用Visual Studio 2013 开发PHP5.6项目

    原文如何使用Visual Studio开发PHP项目 在windows下开发php除了记事本 DW 以及一帮Zend studio,Eclipse,NetBeans之流以外,个人感觉还是vsiual ...