dakodev

Untitled

Nov 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void solve(int n) {
  4. if (n == 1) {
  5. printf("1 = 0! * 1\n");
  6. return;
  7. }
  8.  
  9. int factorials[n];
  10. factorials[0] = 1;
  11. int fmax = 1;
  12. for (; fmax < n; fmax++) {
  13. int fac = (fmax + 1) * factorials[fmax - 1];
  14. if (fac >= n) {
  15. fmax--;
  16. break;
  17. }
  18. factorials[fmax] = fac;
  19. }
  20.  
  21. printf("%d = ", n);
  22. for (int i = fmax; i >= 0; i--) {
  23. int fac = factorials[i];
  24. int s = n / fac;
  25. printf("%d! * %d", (i + 1), s);
  26. n -= s * fac;
  27. if (i > 0)
  28. printf(" + ");
  29. }
  30. putchar('\n');
  31. }
  32.  
  33. int main(void) {
  34. solve(32135);
  35.  
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment