fork(1) 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(vector<string>& canvas, int x, int y, int length, int level) {
  9. if (level == 0) {
  10. for (int i = 0; i < length; i++) {
  11. canvas[y][x + i] = '#';
  12. }
  13. return;
  14. }
  15.  
  16. int segment = length / 3;
  17.  
  18. // Lewa część
  19. cantor(canvas, x, y, segment, level - 1);
  20.  
  21. // Prawa część
  22. cantor(canvas, x + 2 * segment, y, segment, level - 1);
  23. }
  24.  
  25. int main() {
  26. int level;
  27. cout << "Podaj stopień zbioru Cantora (np. 3 lub 6): ";
  28. cin >> level;
  29.  
  30. int width = 1;
  31. for (int i = 0; i < level; i++) width *= 3;
  32.  
  33. vector<string> canvas(level, string(width, ' '));
  34.  
  35. for (int i = 0; i < level; i++) {
  36. cantor(canvas, 0, i, width, i);
  37. }
  38.  
  39. // Wyświetlenie
  40. for (const auto& row : canvas) {
  41. cout << row << endl;
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5276KB
stdin
3
stdout
Podaj stopień zbioru Cantora (np. 3 lub 6): ###########################
#########         #########
###   ###         ###   ###