---
# System prepended metadata

title: '(UVA11470) Square Sums :'
tags: [CPP(CPE)]

---

# (UVA11470) Square Sums :  

UVA11470_Square_Sums.cpp


```cpp=
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, case_num = 1;
    while (cin >> n && n != 0) {
        vector<vector<int>> matrix(n, vector<int>(n));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> matrix[i][j];
            }
        }

        vector<int> sums((n + 1) / 2, 0);
        for (int layer = 0; layer < (n + 1) / 2; layer++) {
            if (layer == n - layer - 1) {
                sums[layer] += matrix[layer][layer]; // Single center element
            } else {
                for (int i = layer; i < n - layer; i++) {
                    sums[layer] += matrix[layer][i]; // Top row
                    sums[layer] += matrix[n - layer - 1][i]; // Bottom row
                }
                for (int i = layer + 1; i < n - layer - 1; i++) {
                    sums[layer] += matrix[i][layer]; // Left column
                    sums[layer] += matrix[i][n - layer - 1]; // Right column
                }
            }
        }

        cout << "Case " << case_num++ << ":";
        for (int sum : sums) {
            cout << " " << sum;
        }
        cout << endl;
    }
    return 0;
}
```
![image](https://hackmd.io/_uploads/rkDn-bmRA.png)
![image](https://hackmd.io/_uploads/S1BgfZXRR.png)
