fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <climits>
  4. #include <algorithm>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. void solve() {
  10. string s;
  11. cin >> s;
  12. int n = s.size();
  13. double min_cost = LONG_MAX;
  14. int min_len = 0;
  15.  
  16. // Check all possible substrings
  17. for (int i = 0; i < n; ++i) {
  18. for (int j = i; j < n; ++j) {
  19. string current_str = s.substr(i, j - i + 1);
  20. long num = stol(current_str);
  21. int sum = 0;
  22. for (char c : current_str) {
  23. sum += c - '0';
  24. }
  25. double cost = (double)num / sum;
  26. if (cost < min_cost) {
  27. min_cost = cost;
  28. min_len = current_str.size();
  29. } else if (cost == min_cost) {
  30. if (current_str.size() > min_len) {
  31. min_len = current_str.size();
  32. }
  33. }
  34. }
  35. }
  36. cout << n - min_len << endl;
  37. }
  38.  
  39. int main() {
  40. int t;
  41. cin >> t;
  42. while (t--) {
  43. solve();
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 5288KB
stdin
4
666
13700
102030
7
stdout
2
4
4
0