Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	out := make([][]string, 100)
	for i := range out {
		out[i] = make([]string, 100)
	}
	dx := []int{0, 1, 0, -1}
	dy := []int{-1, 0, 1, 0}
	scanner.Scan()
	a, _ := strconv.Atoi(scanner.Text())
	for a > 0 {
		a--
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				out[i][j] = " "
			}
			out[i][b] = "0"
		}
		if b == 1 {
			fmt.Println("#")
			if a != 0 {
				fmt.Println()
			}
			continue
		}
		if b == 2 {
			fmt.Println("##")
			if a != 0 {
				fmt.Println()
				continue
			}
		}
		row := b -1
		col := 0
		dir := 0
		len := 0
		out[row][col] = "#"
		for {
			nrow := row+dy[dir%4]
			ncol := col+dx[dir%4]
			if dir%4 == 0 && nrow == 0 || dir%4==3&&ncol==0 || dir%4==2&&nrow==b-1 || dir%4==1&&ncol==b-1 {
				row=nrow
				col=ncol
				out[row][col]="#"
				dir++
				len = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if len < 3 {
					break
				}
				dir++
				len = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				len++
			}
		}
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				fmt.Printf("%s", out[i][j])
			}
			fmt.Println()
		}
		if a != 0 {
			fmt.Println()
		}
	}
}

C++: #include "bits/stdc++.h"
using namespace std;

const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };
int main() {
	int D; cin >> D;
	while (D--) {
		int N; cin >> N;
		vector<vector<char>>field(N, vector<char>(N, ' '));
		int way = 3;
		int nx = 0;
		int ny = N - 1;
		bool flag = true;
		while (flag) {
			field[ny][nx] = '#';
			if (N == 1)break;
			for (int i = 0; i < 2; ++i) {
				int nextx = nx + dx[way];
				int nexty = ny + dy[way];
				if (nextx >= 0 && nextx < N&&nexty >= 0 && nexty < N) {
					bool aok = true;
					for (int w = -1; w <= 1; ++w) {
						int nextway = (way + 4 + w) % 4;
						int next2x = nextx + dx[nextway];
						int next2y = nexty + dy[nextway];
						if (next2x >= 0 && next2x < N&&next2y >= 0 && next2y < N) {
							if (field[next2y][next2x] == '#') {
								aok = false;
							}
						}
					}
					if(!aok){
						if(i==1)flag = false;
						
					}
					else {
						nx = nextx;
						ny = nexty;
						break;
					}
				}
				way = (way + 3) % 4;
			}
			
		}
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				cout << field[i][j];
			}
			cout << endl;
		}
		if(N)cout << endl;
	}
	return 0;
}

C++: 
int n;
char G[100][100]; // grid
static int di[4] = {-1, 0, 1, 0};
static int dj[4] = {0, 1, 0, -1};

int main(){
    int dir, width, i, j, a, w;
    scanf("%d", &n);

    for ( i = 0; i < n; i++ )
        for ( j = 0; j < n; j++ ) G[i][j] = ' ';

    width = n - 1;
    i = width; j = dir = 0;

    for ( a = 1; a <= (n + n % 2); a++, dir = (dir + 1) % 4 ){
        for ( w = 0; w <= width; w++ ) G[i+di[dir]*w][j+dj[dir]*w] = '#';
        i += di[dir] * width;
        j += dj[dir] * width;
        if ( a % 2 == 1 && a >= 3 ) width -= 2;
    }
    
    for ( i = 0; i < n; i++ ){
        for ( j = 0; j < n; j++ ) printf("%c", G[i][j]);
        printf("\n");
    }
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF= 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)

double cross(const xy_t &a, const xy_t &b){
	return imag(conj(a) * b);
}

double dot(const xy_t &a, const xy_t &b){
	return real(conj(a) * b);
}

xy_t projection(const line &l, const xy_t &p){
	double t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
	return l.first + t * (l.first - l.second);
}

bool intersectSP(const line &l, const xy_t &p){
	return abs(l.first- p) + abs(l.second - p) - abs(l.first - l.second) < EPS;
}

double distanceSP(line s, xy_t p){
	const xy_t r = projection(s, p);
	if(intersectSP(s, r)) return abs(r - p);
	return min(abs(s.first - p), abs(s.second - p));
}

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
char field[200][200];

int main(){
	int n, d;
	cin >> d;
	rep(t, d){
		cin >> n;
		if(n == 2){
			cout << "##\n# \n";
			continue;
		}
		fill(&field[0][0], &field[199][199] + 1, '#');
		rep(i, n + 2){
			rep(j, n + 2) field[i+1][j+1] = ' ';
		}
		int x = 2;
		int y = 2 + n - 1;
		int cnt = 0;
		int dire = 0;
		while(true){
			cnt = 0;
			while(field[y + 2 * dy[dire]][x + 2 * dx[dire]] != '#'){
				cnt++;
				field[y][x] = '#';
				y += dy[dire];
				x += dx[dire];
			}
			field[y][x] = '#';
			if(cnt < 2) break;
			dire = (dire + 1) % 4;
		}
		rep(i, n){
			rep(j, n) cout << field[i+2][j+2];
			cout << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#define X '#'

using namespace std;

vector<string> Init(int);
void DrawSpiral(vector<string>&, int);
int DrawTopLine(vector<string>&, int&, int&, int);
int DrawBottomLine(vector<string>&, int&, int&, int);
int DrawLeftLine(vector<string>&, int&, int&, int);
int DrawRightLine(vector<string>&, int&, int&, int);

int main(){
  int i, j, n, m;
  vector<string> data;

  cin >> n;

  for(i=0; i<n; ++i){
    cin >> m;
    data = Init(m);
    DrawSpiral(data, m);

    for(j=0; j<data.size(); ++j)
      cout << data[j] << endl;

    if(i != n-1) cout << endl;
  }
  return 0;
}

vector<string> Init(int n){
  const string str(n, ' ');
  vector<string> data;
  for(int i=0; i<n; ++i) data.push_back(str);
  return data;
}

void DrawSpiral(vector<string>& data, int n){
  if(n == 1){
    data[0][0] = X;
    return;
  }
  if(n == 2){
    data[0][0] = X;
    data[1][0] = X;
    data[0][1] = X;
    return;
  }

  int i, j;
  i = n-1;
  j = 0;
  while(1){
    if(DrawLeftLine(data, i, j, n) == 1) return;
    if(DrawTopLine(data, i, j, n) == 1) return;
    if(DrawRightLine(data, i, j, n) == 1) return;
    if(DrawBottomLine(data, i, j, n) == 1) return;
  }
}

int DrawTopLine(vector<string>& data, int& i, int& j, int n){
  if(j == n-1 || (j <= n-3 && data[i][j+2] == X)) return 1;
  if(n%2 == 0 && data[i][j+3] == X){
    data[i][j+1] = X;
    return 1;
  }
  while(j <= n-1 && data[i][j+2] != X){
    data[i][j] = X;
    if(j == n-1 || (j <= n-3 && data[i][j+2] == X)) return 0;
    ++j;
  }
  return 0;
}

int DrawBottomLine(vector<string>& data, int& i, int& j, int n){
  if(j == 0 || (j >= 2 && data[i][j-2] == X)) return 1;
  if(n%2 == 0 && data[i][j-3] == X){
    data[i][j-1] = X;
    return 1;
  }
  while(j >= 0 && data[i][j-2] != X){
    data[i][j] = X;
    if(j == 0 || (j >= 2 && data[i][j-2] == X)) return 0;
    --j;
  }
}

int DrawLeftLine(vector<string>& data, int& i, int& j, int n){
  if(i == 0 || (i >= 2 && data[i-2][j] == X)) return 1;
  if(n%2 == 0 && data[i-3][j] == X){
    data[i-1][j] = X;
    return 1;
  }
  while(1){
    data[i][j] = X;
    if(i == 0 || (i >= 2 && data[i-2][j] == X)) return 0;
    --i;
  }
}

int DrawRightLine(vector<string>& data, int& i, int& j, int n){
  if(i == n-1 || (i <= n-3 && data[i+2][j] == X)) return 1;
  if(n%2 == 0 && data[i+3][j] == X){
    data[i+1][j] = X;
    return 1;
  }
  while(1){
    data[i][j] = X;
    if(i == n-1 || (i <= n-3 && data[i+2][j] == X)) return 0;
    ++i;
  }
}