fork download
  1. #include <iostream>
  2. #include <set>
  3. #include "math.h"
  4.  
  5. int digitcount(int num) {
  6. int count = 1;
  7. while (num /= 10)
  8. count++;
  9. return count;
  10. }
  11.  
  12. int getdigit(int num, int index) {
  13. return (int)(num / pow(10, digitcount(num) - index - 1)) % 10;
  14. }
  15.  
  16. int func(int num) {
  17. std::set<int> digits;
  18. for (int i = digitcount(num) - 1; i >= 0; i--) {
  19. int digit = getdigit(num, i);
  20. if (digit && !digits.count(digit)) {
  21. if (func(num - digit))
  22. return digit;
  23. else
  24. digits.insert(digits.end(), digit);
  25. }
  26. }
  27. }
  28.  
  29. int main() {
  30. long long number;
  31. std::cin >> number;
  32. int value = func(number);
  33. if (value)
  34. std::cout << value;
  35. else
  36. std::cout << "NO";
  37. return 0;
  38. }
Success #stdin #stdout 0.35s 662528KB
stdin
23014532
stdout
2