fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. // Funkcja rekurencyjna rysująca zbiór Cantora
  6. void cantor(vector<string>& canvas, int level, int left, int right, int height) {
  7. if (level == 0) {
  8. // rysujemy odcinek
  9. for (int i = left; i <= right; i++) {
  10. canvas[height][i] = '#';
  11. }
  12. return;
  13. }
  14.  
  15. int third = (right - left + 1) / 3;
  16.  
  17. // Lewa część
  18. cantor(canvas, level - 1, left, left + third - 1, height + 1);
  19.  
  20. // Prawa część
  21. cantor(canvas, level - 1, right - third + 1, right, height + 1);
  22. }
  23.  
  24. int main() {
  25. int n;
  26. cout << "Podaj stopien zbioru Cantora (np. 3 lub max 6): ";
  27. cin >> n;
  28.  
  29. if (n < 0 || n > 6) {
  30. cout << "Stopien musi byc z zakresu 0-6!" << endl;
  31. return 1;
  32. }
  33.  
  34. int width = 1;
  35. for (int i = 0; i < n; i++) {
  36. width *= 3; // 3^n
  37. }
  38.  
  39. vector<string> canvas(n + 1, string(width, ' '));
  40.  
  41. // Rysujemy pierwszy (pełny) odcinek
  42. for (int i = 0; i < width; i++) {
  43. canvas[0][i] = '#';
  44. }
  45.  
  46. // Rekurencja
  47. cantor(canvas, n, 0, width - 1, 0);
  48.  
  49. // Wyświetlanie
  50. for (const auto& row : canvas) {
  51. cout << row << endl;
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5320KB
stdin
6
stdout
Podaj stopien zbioru Cantora (np. 3 lub max 6): #########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
# #   # #         # #   # #                           # #   # #         # #   # #                                                                                 # #   # #         # #   # #                           # #   # #         # #   # #                                                                                                                                                                                                                                                   # #   # #         # #   # #                           # #   # #         # #   # #                                                                                 # #   # #         # #   # #                           # #   # #         # #   # #