Advertisement
GilsonMuniz

1936 - Fatorial

May 1st, 2020
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int fatorial(int v[], int n)
  6. {
  7.     int i;
  8.  
  9.     v[0] = 1;
  10.     for(i = 1; i <= n && i * v[i - 1] <= n; i++)
  11.         v[i] = i * v[i - 1];
  12.  
  13.     return i;
  14. }
  15.  
  16. int gula(int v[], int tam, int n)
  17. {
  18.     int s = 0;
  19.  
  20.     bool achou = false;
  21.  
  22.     s = v[tam - 1];
  23.     int ans = 1;
  24.     while(!achou)
  25.     {
  26.         if(s == n)
  27.             achou = true;
  28.         else if(n - s < v[tam / 2])
  29.         {
  30.             int i;
  31.             for(i = 1; i < tam / 2 && s + v[i] <= n; i++);
  32.             s += v[i - 1];
  33.  
  34.             ans++;
  35.         }
  36.         else
  37.         {
  38.             int i;
  39.             for(i = tam / 2; i < tam && s + v[i] <= n; i++);
  40.             s += v[i - 1];
  41.  
  42.             ans++;
  43.         }
  44.     }
  45.  
  46.     return ans;
  47. }
  48.  
  49. int main()
  50. {
  51.     int n;
  52.  
  53.     cin >> n;
  54.     int v[n];
  55.  
  56.     int tam = fatorial(v, n);
  57.     int ans = gula(v, tam, n);
  58.  
  59.     cout << ans << '\n';
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement