a53

Sierpinski

a53
Oct 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <cmath>
  4. #define DMAX 730
  5. using namespace std;
  6.  
  7. vector <vector<bool> > a(DMAX);
  8. int n,p3lan;
  9.  
  10. /// Functia care initializeaza "matricea":
  11. void init()
  12. {
  13. for (int i=1;i<DMAX;++i)
  14. a[i].resize(DMAX);
  15. }
  16.  
  17. /// Functia recursiva:
  18. void mat3(int x,int y,int p3lan)
  19. {
  20. int pe3=p3lan/3; /// "Umplem" cu 1 patratul din mijloc:
  21. for(int i=x+pe3;i<=x+2*pe3-1;++i)
  22. for(int j=y+pe3;j<=y+2*pe3-1;++j)
  23. a[i][j]=true;
  24.  
  25. /// Dupa ce am format patrate de lungime minima (1), ne oprim:
  26. if (pe3==1)
  27. return;
  28. /// Umplem cele 8 patrate:
  29. mat3(x , y , pe3);
  30. mat3(x , y + pe3, pe3);
  31. mat3(x , y + 2 * pe3, pe3);
  32. mat3(x + pe3, y , pe3);
  33. mat3(x + pe3, y + 2 * pe3, pe3);
  34. mat3(x + 2 * pe3, y , pe3);
  35. mat3(x + 2 * pe3, y + pe3, pe3);
  36. mat3(x + 2 * pe3, y + 2 * pe3, pe3);
  37. }
  38.  
  39. int main()
  40. {
  41. ifstream f("sierpinski.in");
  42. f>>n;
  43. f.close();
  44. p3lan=pow(3, n);
  45. init();
  46. mat3(1,1,p3lan);
  47. ofstream g("sierpinski.out");
  48. for(int i=1;i<=p3lan;++i)
  49. {
  50. for(int j=1;j<=p3lan;++j)
  51. g<<a[i][j]<<' ';
  52. g<<'\n';
  53. }
  54.  
  55. g.close();
  56. return 0;
  57. }
Add Comment
Please, Sign In to add comment