fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool has_zero(const string &s) {
  5. for (char c : s)
  6. if (c == '0') return true;
  7. return false;
  8. }
  9.  
  10. string next(const string &s) {
  11. if (has_zero(s)) return "0";
  12. long long prod = 1;
  13. for (char c : s)
  14. prod *= (c - '0');
  15. return to_string(prod);
  16. }
  17.  
  18. int main() {
  19. srand(time(0));
  20. int step = 0;
  21. string best;
  22. for (int i = 8; i <= 50; ++i) {
  23. for (int j = 1; j <= 1000; ++j) {
  24. string s = "";
  25. for (int k = 0; k < i; ++k) {
  26. int x = rand() % 9 + 1;
  27. s += to_string(x);
  28. }
  29. string tmp = s;
  30. int li = 0;
  31. while (tmp.size() > 1) {
  32. tmp = next(tmp);
  33. ++li;
  34. }
  35. if (li > step) {
  36. step = li;
  37. best = s;
  38. }
  39. }
  40. }
  41. cout << best << "\n";
  42. cout << step << "\n";
  43. }
  44.  
Success #stdin #stdout 0.27s 5292KB
stdin
Standard input is empty
stdout
9444724692729
10