Advertisement
NotAlpha102

Stack problem

Mar 31st, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef unsigned long long ull;
  4. ull factorial(ull n);
  5. main()
  6. {
  7.     ios_base::sync_with_stdio(false);
  8.     cin.tie(NULL);
  9.     ull n, i, fact[21], j;
  10.     cin>>n;
  11.     stack<ull>num_stack;
  12. //To store factorials upto 20 in the array.
  13.     for(i=0; i<21; i++)
  14.     {
  15.         fact[i]=factorial(i+1);
  16.     }
  17.     for(j=19; j>=0; j--)
  18.     {
  19.         if(fact[j]<=n)
  20.         {
  21.             num_stack.push(j+1);
  22.             n=n-fact[j];
  23.         }
  24.     }
  25.     cout<<num_stack.top()<<' '<<n;
  26.  
  27. }
  28. ull factorial(ull n)
  29. {//factorial(0)= 1
  30.     if(n==0)
  31.         return 1;
  32.     else
  33.         return n*factorial(n-1);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement