fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Funkcja generująca linię zbioru Cantora
  6. string cantorLine(int level) {
  7. if (level == 0)
  8. return "-";
  9.  
  10. string prev = cantorLine(level - 1);
  11. string space(prev.length(), ' ');
  12.  
  13. return prev + space + prev;
  14. }
  15.  
  16. // Funkcja rysująca cały zbiór
  17. void drawCantor(int level) {
  18. string line = cantorLine(level);
  19.  
  20. for (int i = 0; i <= level; i++) {
  21. cout << line << endl;
  22.  
  23. // przygotuj następną linię (usuń środkowe fragmenty)
  24. string next = line;
  25. int len = next.length();
  26.  
  27. for (int j = 0; j < len; j++) {
  28. int k = j;
  29. while (k > 0) {
  30. if (k % 3 == 1) {
  31. next[j] = ' ';
  32. break;
  33. }
  34. k /= 3;
  35. }
  36. }
  37.  
  38. line = next;
  39. }
  40. }
  41.  
  42. int main() {
  43. int stopien = 3;
  44.  
  45. cout << "Podaj stopień zbioru Cantora (np. 3 lub 6): ";
  46. cin >> stopien;
  47.  
  48. if (stopien < 0) {
  49. cout << "Stopień musi być >= 0!" << endl;
  50. return 1;
  51. }
  52.  
  53. drawCantor(stopien);
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Podaj stopień zbioru Cantora (np. 3 lub 6): - -   - -         - -   - -
- -   - -         - -   - -
- -   - -         - -   - -
- -   - -         - -   - -