Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NUM 10005
  3. using namespace std;
  4. int dp[NUM];
  5. int n, num;
  6.  
  7. int main()
  8. {
  9. for(int i = 1; i <= 4; ++i) dp[i] = i;
  10.  
  11. for (int i = 4; i < NUM; i++)
  12. {
  13. dp[i] = i;
  14.  
  15. for (int x = 1; x <= ceil(sqrt(i)); x++)
  16. {
  17. int aux = x * x;
  18. if (aux > i)
  19. break;
  20. else
  21. dp[i] = min(dp[i], 1 + dp[i - aux]);
  22. }
  23. }
  24.  
  25. cin >> n;
  26. for(int i = 0; i < n; ++i)
  27. {
  28. cin >> num;
  29. cout << dp[num] << '\n';
  30. }
  31. return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement