fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n, a[100][100];
  5.  
  6. int tinhtong(int a[100][100], int n, int x, int y)
  7. {
  8. int sum = 0;
  9.  
  10. for(int i = -1 ; i <= 1 ; i ++)
  11. for (int j = -1; j <= 1; j++)
  12. {
  13. if (y + i >= 0 && x + j < n && y + i < n && x + j >= 0)
  14. sum += a[y + i][x + j];
  15. }
  16. return sum - a[y][x];
  17. }
  18.  
  19. int main()
  20. {
  21. cin >> n;
  22. for (int i = 0; i < n; i++)
  23. for (int j = 0; j < n; j++)
  24. cin >> a[i][j];
  25. cout << tinhtong(a, n, 2, 1);
  26. return 0;
  27. }
  28. /*
  29. 3
  30. 1 2 3
  31. 4 5 6
  32. 7 8 9
  33. */
Success #stdin #stdout 0.01s 5488KB
stdin
3
1 2 3 
4 5 6
7 8 9
stdout
27