fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Funkcja rekurencyjna rysująca zbiór Cantora
  5. void cantor(int x, int y, int length, int depth, char canvas[][200]) {
  6. if (depth == 0) return;
  7.  
  8. // Rysuj linię
  9. for (int i = x; i < x + length; i++) {
  10. canvas[y][i] = '#';
  11. }
  12.  
  13. // Rekurencja dla lewej i prawej części
  14. int newLength = length / 3;
  15.  
  16. cantor(x, y + 2, newLength, depth - 1, canvas);
  17. cantor(x + 2 * newLength, y + 2, newLength, depth - 1, canvas);
  18. }
  19.  
  20. int main() {
  21. int depth;
  22.  
  23. cout << "Podaj stopien zbioru Cantora (np. 3 lub 6): ";
  24. cin >> depth;
  25.  
  26. const int WIDTH = 200;
  27. const int HEIGHT = 50;
  28.  
  29. // Tablica do rysowania
  30. char canvas[HEIGHT][WIDTH];
  31.  
  32. // Wypełnij spacjami
  33. for (int i = 0; i < HEIGHT; i++)
  34. for (int j = 0; j < WIDTH; j++)
  35. canvas[i][j] = ' ';
  36.  
  37. // Start rysowania
  38. cantor(0, 0, WIDTH, depth, canvas);
  39.  
  40. // Wyświetlanie
  41. for (int i = 0; i < HEIGHT; i++) {
  42. for (int j = 0; j < WIDTH; j++) {
  43. cout << canvas[i][j];
  44. }
  45. cout << endl;
  46. }
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5320KB
stdin
3
stdout
Podaj stopien zbioru Cantora (np. 3 lub 6): ########################################################################################################################################################################################################
                                                                                                                                                                                                        
##################################################################                                                                  ##################################################################  
                                                                                                                                                                                                        
######################                      ######################                                                                  ######################                      ######################