Problem p03090 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    f = 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  if (f)
    x = -x;
}

int sum[20], e[20][20];
int n;

/*void dfs(int x, int y)
{
        if(x == n)
        {
                int t = sum[1], flg = 1;
                for(int i=1; i<=n; i++)
                        if(sum[i] != t)
                                flg = 0;
                if(flg)
                {
                        cout<<t<<" : "<<endl;
                        for(int i=1; i<=n; i++)
                        {
                                for(int j=1; j<=n; j++) cout<<e[i][j]<<" ";
                                cout<<endl;
                        }
                }
        }
        else if(y == n+1) dfs(x+1, x+2);
        else
        {
                dfs(x, y+1);
                sum[x] += y;
                sum[y] += x;
                e[x][y] = e[y][x] = 1;
                dfs(x, y+1);
                sum[x] -= y;
                sum[y] -= x;
                e[x][y] = e[y][x] = 0;
        }
}*/

int main() {
  read(n);
  if (n & 1) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (i + j == n || i == j)
          e[i][j] = 0;
        else
          e[i][j] = 1;
  } else {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (i + j == n + 1 || i == j)
          e[i][j] = 0;
        else
          e[i][j] = 1;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (e[i][j])
        cnt++;
  printf("%d\n", cnt);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (e[i][j])
        printf("%d %d\n", i, j);
  // dfs(1, 2);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    f = 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  if (f)
    x = -x;
}

int sum[270], e[270][270];
int n;

/*void dfs(int x, int y)
{
        if(x == n)
        {
                int t = sum[1], flg = 1;
                for(int i=1; i<=n; i++)
                        if(sum[i] != t)
                                flg = 0;
                if(flg)
                {
                        cout<<t<<" : "<<endl;
                        for(int i=1; i<=n; i++)
                        {
                                for(int j=1; j<=n; j++) cout<<e[i][j]<<" ";
                                cout<<endl;
                        }
                }
        }
        else if(y == n+1) dfs(x+1, x+2);
        else
        {
                dfs(x, y+1);
                sum[x] += y;
                sum[y] += x;
                e[x][y] = e[y][x] = 1;
                dfs(x, y+1);
                sum[x] -= y;
                sum[y] -= x;
                e[x][y] = e[y][x] = 0;
        }
}*/

int main() {
  read(n);
  if (n & 1) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (i + j == n || i == j)
          e[i][j] = 0;
        else
          e[i][j] = 1;
  } else {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (i + j == n + 1 || i == j)
          e[i][j] = 0;
        else
          e[i][j] = 1;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (e[i][j])
        cnt++;
  printf("%d\n", cnt);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (e[i][j])
        printf("%d %d\n", i, j);
  // dfs(1, 2);
  return 0;
}