Advertisement
Guest User

Magic

a guest
Nov 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream in ("magic.in");
  7. ofstream out ("magic.out");
  8.  
  9. int getvalue(pair<int,int> a, int n){
  10. int x = a.first;
  11. int y = a.second;
  12. int x2 = (n / 2);
  13. int y2 = - (n / 2);
  14. int value = 1;
  15. while(x2 + y2 != (x + y)){
  16. x2++;
  17. y2++;
  18. value += n;
  19. }
  20. while(x2 != x && y2 != y){
  21. y2++;
  22. x2--;
  23. value++;
  24. }
  25. return value;
  26. }
  27.  
  28. pair<int,int> realpos(pair<int, int> a, int n){
  29. int x = a.first;
  30. int y = a.second;
  31.  
  32. if((x + y) % 2 == 0)
  33. return {x, y};
  34. else{
  35. if(y < x) {
  36. if(y < (n - x))
  37. return {x, y + n};
  38. else
  39. return {x - n, y};
  40. } else {
  41. if(y < (n - x))
  42. return {x + n, y};
  43. else
  44. return {x, y - n};
  45. }
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. int n;
  52. in >> n;
  53. for(int i = 0; i < n; i++) {
  54. for(int j = 0; j < n; j++) {
  55. out << getvalue(realpos({i, j}, n), n) << " ";
  56. }
  57. out << '\n';
  58. }
  59.  
  60. int N, l, c;
  61. in >> N >> l >> c;
  62. out << getvalue(realpos({l - 1, c - 1}, N), N);
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement