Guest User

Untitled

a guest
Aug 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. How to save space and time with a mirrored NxN array
  2. [ 0 3 9 2 ]
  3. [ 3 0 5 6 ]
  4. [ 9 5 0 1 ]
  5. [ 2 6 1 0 ]
  6.  
  7. #include <QVector>
  8. #include <QDebug>
  9. #include <QString>
  10.  
  11. class MirroredArray
  12. {
  13. public:
  14. MirroredArray(int sideLength)
  15. {
  16. values.fill(0, sideLength*(sideLength+1)/2);
  17. this->s = sideLength;
  18. }
  19.  
  20. int get(int r, int c)
  21. {
  22. if(c > r)
  23. {
  24. return values.at(s*r-(r-1)*r/2 + c-r);
  25. }
  26. else
  27. {
  28. return values.at(s*c-(c-1)*c/2 + r-c);
  29. }
  30. }
  31.  
  32. void set(int r, int c, int value)
  33. {
  34. if(c > r)
  35. {
  36. values[s*r-(r-1)*r/2 + c-r] = value;
  37. }
  38. else
  39. {
  40. values[s*c-(c-1)*c/2 + r-c] = value;
  41. }
  42. }
  43. int getSide()
  44. {
  45. return s;
  46. }
  47.  
  48. QString contentsToString()
  49. {
  50. QString temp = "(" + QString::number(values.size()) + ") - ";
  51. for(int i = 0; i<values.size(); i++)
  52. temp += QString::number(i) + ", ";
  53. return temp;
  54. }
  55.  
  56. private:
  57. QVector <int> values;
  58. int s;
  59. };
  60.  
  61. 01234 (n=5)
  62. 5678
  63. 901
  64. 23
  65. 4
Add Comment
Please, Sign In to add comment