Little_hobbit

Задача о калькулятре - Динамика

Jun 17th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int main( )
  7. {
  8.     int n;
  9.     cin >> n;
  10.  
  11.     int g[n+1];
  12.     g[1] = 0;
  13.     g[2] = 1;
  14.     g[3] = 1;
  15.  
  16.     for (int i = 4; i <= n; ++i)
  17.     {
  18.         if ((i % 3 == 0) && (i % 2 == 0))
  19.             g[i] = min(g[i-1], min(g[i/3], g[i/2])) + 1;
  20.         else if (i % 2 == 0)
  21.             g[i] = min(g[i-1], g[i/2]) + 1;
  22.         else if (i % 3 == 0)
  23.             g[i] = min(g[i-1], g[i/3]) + 1;
  24.         else
  25.             g[i] = g[i-1] + 1;
  26.     }
  27.  
  28.     cout << g[n];
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment