Advertisement
Saleh127

UVA 324

Oct 30th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define MAX 100000
  5.  
  6. int multiply(int x, int res[], int res_size);
  7.  
  8. void factorial(int n)
  9. {
  10. int res[MAX];
  11. int ans[10]={0};
  12. res[0] = 1;
  13. int res_size = 1;
  14.  
  15. for (int x=1; x<=n; x++)
  16. {
  17. res_size = multiply(x, res, res_size);
  18. }
  19. for (int i=res_size-1; i>=0; i--)
  20. {
  21. int zz=res[i];
  22. ans[zz]++;
  23. }
  24.  
  25.  
  26. printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n",ans[0],ans[1],ans[2],ans[3],ans[4]);
  27. printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d\n",ans[5],ans[6],ans[7],ans[8],ans[9]);
  28.  
  29. }
  30.  
  31. int multiply(int x, int res[], int res_size)
  32. {
  33. int carry = 0;
  34.  
  35. for (int i=0; i<res_size; i++)
  36. {
  37. int prod = res[i] * x + carry;
  38. res[i] = prod % 10;
  39. carry = prod/10;
  40. }
  41.  
  42. while (carry)
  43. {
  44. res[res_size] = carry%10;
  45. carry = carry/10;
  46. res_size++;
  47. }
  48. return res_size;
  49. }
  50.  
  51. int main()
  52. {
  53.  
  54. int n;
  55. while(cin>>n && n)
  56. {
  57. cout<<n<<"! --"<<endl;
  58. factorial(n);
  59. }
  60.  
  61. return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement