fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int inv(int x)
  5. {
  6. int r=0;
  7. while(x)
  8. {
  9. r*=10;
  10. r+=x%10;
  11. x/=10;
  12. }
  13. return r;
  14. }
  15. int n,dp[1000005];
  16. int call(int x)
  17. {
  18. if(x==n) return 0;
  19. if(x>n) return n;
  20. if(dp[x]!=-1) return dp[x];
  21. int ans=n;
  22. ans=min(ans,1+call(x+1));
  23. if(inv(x)>x&&inv(x)<=n) ans=min(ans,1+call(inv(x)));
  24. return dp[x]=ans;
  25. }
  26.  
  27. int main()
  28. {
  29. int t;
  30. cin >> t ;
  31. for(int ct = 1 ; ct <= t ; ct++)
  32. {
  33. cin >> n;
  34. for(int i=0;i<=n+5;i++) dp[i]=-1;
  35. cout << "Case #" << ct << ": " << call(0) << "\n" ;
  36. }
  37. }
  38.  
Runtime error #stdin #stdout 0s 15424KB
stdin
1
176543
stdout
Standard output is empty