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 = 0;
  21. int sum = 0;
  22. for (char c : current_str) {
  23. num = num * 10 + (c - '0');
  24. sum += (c - '0');
  25. }
  26. double cost = (double)num / sum;
  27. if (cost < min_cost) {
  28. min_cost = cost;
  29. min_len = current_str.size();
  30. } else if (cost == min_cost) {
  31. if (current_str.size() > min_len) {
  32. min_len = current_str.size();
  33. }
  34. }
  35. }
  36. }
  37. cout << n - min_len << endl;
  38. }
  39.  
  40. int main() {
  41. int t;
  42. cin >> t;
  43. while (t--) {
  44. solve();
  45. }
  46. return 0;
  47. }
Success #stdin #stdout 0s 5288KB
stdin
4
666
13700
102030
7
stdout
2
4
4
0