fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. // Funkcja generująca jedną linię zbioru Cantora
  6. void generateCantor(vector<char>& line, int start, int length) {
  7. if (length < 3) return;
  8.  
  9. int third = length / 3;
  10.  
  11. // usuń środkową część (zamień na spacje)
  12. for (int i = start + third; i < start + 2 * third; i++) {
  13. line[i] = ' ';
  14. }
  15.  
  16. // rekurencja dla lewej i prawej części
  17. generateCantor(line, start, third);
  18. generateCantor(line, start + 2 * third, third);
  19. }
  20.  
  21. int main() {
  22. int stopien;
  23.  
  24. cout << "Podaj stopien zbioru Cantora (np. 3 lub 6): 3 ";
  25. cin >> stopien;
  26.  
  27. if (stopien < 0 || stopien > 8) {
  28. cout << "Podaj stopien w zakresie 0–8!" << endl;
  29. return 1;
  30. }
  31.  
  32. // długość = 3^stopien
  33. int length = 1;
  34. for (int i = 0; i < stopien; i++) {
  35. length *= 3;
  36. }
  37.  
  38. // tworzymy linię początkową
  39. vector<char> line(length, '-');
  40.  
  41. // generujemy zbiór Cantora
  42. generateCantor(line, 0, length);
  43.  
  44. // wypisujemy wynik
  45. for (char c : line) {
  46. cout << c;
  47. }
  48. cout << endl;
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Podaj stopien zbioru Cantora (np. 3 lub 6): 3 -