[关闭]
@xunuo 2017-01-18T13:35:42.000000Z 字数 1721 阅读 885

Hex Factorial


Time limit2000ms Memory limit 32768kB

高精度

来源:
HDU 2940 Hex Factorial
vjudge A Hex Factorial


Description

The expression N!, reads as the factorial of N, denoting the product of the first N positive integers. If the factorial of N is written in hexadecimal without leading zeros, can you tell us how many zeros are there in it? Take 15! as an example, you should answer "3" because (15)10! = (13077775800)16, and there are 3 zeros in it.

Input

The input contains several cases. Each case has one line containing a non-negative decimal integer N (N ≤ 100). You need to count the zeros in N! in hexadecimal. A negative number terminates the input.

Output

For each non-negative integer N, output one line containing exactly one integer, indicating the number of zeros in N!.

Sample Input

1
15
-1

Sample Output

0
3

题意

该题目的为求某数的阶乘后再将其转换为16进制数,问其16进制数中有多少个0。
多组输入,输入一个数,输出0的个数,当输入负数时结束!
大数的进制转换:一直除以该数,当商为0时结束,转换结果为每次所得的余数。

完整代码

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int a[160],b[160],s[160];///10!的位数未超过160。
  7. int main()
  8. {
  9. int n;
  10. while(scanf("%d",&n)!=EOF)
  11. {
  12. if(n<0)///注意!!!当n<0的时候都要结束!!!
  13. break;
  14. memset(a,0,sizeof(a));
  15. memset(b,0,sizeof(b));
  16. memset(s,0,sizeof(s));
  17. a[0]=1;
  18. int num=1;
  19. int t;
  20. ///计算n的阶乘!!!
  21. for(int i=1;i<=n;i++)
  22. {
  23. int temp=0;///进位!
  24. for(int j=0;j<num;j++)
  25. {
  26. t=i*a[j]+temp;
  27. a[j]=t%10;
  28. temp=t/10;
  29. }
  30. while(temp)
  31. {
  32. a[num]=temp%10;
  33. num++;
  34. temp/=10;
  35. }
  36. }
  37. ///去前导零
  38. for(int i=num;i>0;i--)
  39. {
  40. if(a[i]==0)
  41. num--;
  42. else
  43. break;
  44. }
  45. /*printf("10jz:\n");
  46. for(int i=num;i>=0;i--)
  47. printf("%d",a[i]);
  48. printf("\n");*/
  49. ///将高精度10进制数转为16进制!!
  50. int j=0;
  51. while(a[num]>0)
  52. {
  53. t=0;
  54. for(int i=num;i>=0;i--)
  55. {
  56. t=t*10+a[i];
  57. a[i]=t/16;
  58. t%=16;
  59. if(i==0)
  60. s[j]=t;
  61. }
  62. for(int i=num;i>=0;i--)
  63. {
  64. if(a[i]==0)
  65. num--;
  66. else
  67. break;
  68. }
  69. j++;
  70. }
  71. //printf("%d#\n",j);
  72. /*printf("16jz:\n");
  73. for(int i=j-1;i>=0;i--)
  74. printf("%d",s[i]);
  75. printf("\n");*/
  76. ///计算0的个数
  77. int count=0;
  78. for(int i=j-1;i>=0;i--)
  79. {
  80. if(s[i]==0)
  81. count++;
  82. }
  83. printf("%d\n",count);
  84. }
  85. return 0;
  86. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注