fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // Funkcja rekurencyjna do generowania zbioru Cantora
  8. void cantor(string &line, int left, int right, int level) {
  9. if (level == 0) return;
  10.  
  11. int third = (right - left + 1) / 3;
  12.  
  13. // usuwamy środkową część (zamieniamy na spacje)
  14. for (int i = left + third; i < left + 2 * third; i++) {
  15. line[i] = ' ';
  16. }
  17.  
  18. // rekurencja dla lewej i prawej części
  19. cantor(line, left, left + third - 1, level - 1);
  20. cantor(line, left + 2 * third, right, level - 1);
  21. }
  22.  
  23. int main() {
  24. const int maxLevel = 6;
  25.  
  26. for (int level = 0; level <= maxLevel; level++) {
  27. int length = 1;
  28. for (int i = 0; i < level; i++) {
  29. length *= 3;
  30. }
  31.  
  32. string line(length, '#');
  33.  
  34. cantor(line, 0, length - 1, level);
  35.  
  36. cout << line << endl;
  37. }
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
#
# #
# #   # #
# #   # #         # #   # #
# #   # #         # #   # #                           # #   # #         # #   # #
# #   # #         # #   # #                           # #   # #         # #   # #                                                                                 # #   # #         # #   # #                           # #   # #         # #   # #
# #   # #         # #   # #                           # #   # #         # #   # #                                                                                 # #   # #         # #   # #                           # #   # #         # #   # #                                                                                                                                                                                                                                                   # #   # #         # #   # #                           # #   # #         # #   # #                                                                                 # #   # #         # #   # #                           # #   # #         # #   # #