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, 101)
	}
	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
		l := 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++
				l = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if l < 3 {
					break
				}
				dir++
				l = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				l++
			}
		}
		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<iostream>

using namespace std;

int main()
{
  int d;
  cin>>d;
  for(;d--;)
    {
      int n;
      cin>>n;
      char output[n][n];
      for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
	  output[i][j]=' ';

      int dx[]={-1,0,1,0};
      int dy[]={0,1,0,-1};
      int move=0;
      int cnt=0;
      int x=n-1,y=0;
      for(;;)
	{
	  //	  cout<<x<<" "<<y<<endl;
	  // cout<<"next:"<<x+dx[move]<<" "<<y+dy[move]<<endl;
	  output[x][y]='#';
	  cnt++;
	  bool flag=false;
	  while(x+dx[move]<0 || 
		x+dx[move]>=n ||
		y+dy[move]<0 || 
		y+dy[move]>=n ||
		(
		 x+dx[move]*2>=0 &&
		 x+dx[move]*2<n &&
		 y+dy[move]*2>=0 &&
		 y+dy[move]*2<n &&
		 output[x+dx[move]*2][y+dy[move]*2]=='#'
		 )
		)
	    {
	      /*
		cout<<(x+dx[move]<0)<<endl; 
		cout<<(x+dx[move]>=n)<<endl; 
		cout<<(y+dy[move]<0)<<endl; 
		cout<<(y+dy[move]>=n)<<endl; 
	      */
	      move++;
	      if(move>3)
		move=0;
	      
	      if(cnt<2)
		{
		  flag=true;
		  break;
		}
	      cnt=0;
	    }
	  if(flag)
	    break;
	  x+=dx[move];
	  y+=dy[move];
	  
	}

      for(int i=0;i<n;i++,cout<<endl)
	for(int j=0;j<n;j++)
	  cout<<output[i][j];
      if(d>0)
	cout<<endl;
    }

}

C++: #include <iostream>

#include <string>

using namespace std;

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

int main(){

	int d;

	cin >> d;

	for(int t = 0;t < d;t++){

		if(t) cout << endl;

		char s[100][100];

		int n;

		cin >> n;

		for(int i = 0;i < n;i++){

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

		}

		int x = n - 1,y = 0,dir = 0;

		for(int k = 0;k < n;k++){

			bool flag = false;

			while(1){

				int nx = x + dx[dir],ny = y + dy[dir];

			 if(nx < 0 || nx >= n + 1 || ny < 0 || ny >= n + 1 || s[nx][ny] == '#') break;

s[x][y] = '#';

flag = true;

				x = nx;

				y = ny;

			}

if(x)x -= dx[dir];

if(y)y -= dy[dir];

			if(!flag) break;

			dir = (dir + 1) % 4;

		}

for(int i = 0;i < n;i++) {

for(int j = 0;j < n;j++){

cout << s[i][j];

}

cout << endl;}

	}

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>

#define EPS	1.0e-10
#define rep(idx, max)	for(int idx = 0, idx##Max = max; idx < idx##Max; idx ++)
#define rrep(idx, min, max)	for(int idx = min, idx##Max = max; idx < idx##Max; idx ++)
#define erep(idx, min, max)	for(int idx = min, idx##Max = max; idx <= idx##Max; idx ++)

#define rev(idx, max)	for(int idx = max - 1; idx >= 0; idx --)
#define MAX_N	128
char g_map[MAX_N][MAX_N];

int main()
{
//	static int dx[] = { 0, 1, 0, -1 };
//	static int dy[] = { -1, 0, 1, 0 };
	static int dx[] = { -1, 0, 1, 0 };
	static int dy[] = { 0, -1, 0, 1 };
	int dir;
	int edgeLen;
	int testCases;

	std::cin >> testCases;
	while (testCases)
	{
		int x, y;
		std::cin >> edgeLen;
		::memset(g_map, ' ', sizeof(g_map));
		dir = 0;
		x = edgeLen + 2 - 1;
		y = edgeLen + 2 - 1;
		for (int n = edgeLen + 2; n >= 2; )
		{
			rep(i, n == 2 ? 1 : 2)
			{
				rep(j, n)
				{
					g_map[y][x] = '#';
					::printf("(%d, %d)\n", x, y);
					x += dx[dir];
					y += dy[dir];
				}
				x -= dx[dir];
				y -= dy[dir];
				dir = (dir + 1) & 0x03;
			}
			n -= 2;
		}

		rep(y, edgeLen)
		{
			rep(x, edgeLen)
			{
				::putchar(g_map[y][x]);
			}
			::puts("");
		}
		::puts("");
	}
}

C++: #include <stdio.h>

#define N 100

int main(void){
    int dx[] = {0,1,0,-1};
    int dy[] = {-1,0,1,0};
    int map[N+2][N+2];
    int datas,d,dd,n;
    int x,y;
    
    scanf("%d", &datas);
    for (int i=0; i<datas; i++){
        // get size
        scanf("%d", &n);
        // reset map
        for (int xx=0; xx<n+2; xx++){
            for (int yy=0; yy<n+2; yy++){
                if (xx==0 || xx==n+1 || yy==0 || yy==n+1){
                    map[xx][yy]=-1;
                }else {
                    map[xx][yy]=0;
                }
            }
        }
        x=1; y=n; d=0; dd=1;
        // set spiral
        while(1){
            map[x][y]=1;
            if (map[x+dx[d]][y+dy[d]]==0){
                map[x+dx[dd]][y+dy[dd]]=-1;
            } else{
                if (map[x+dx[dd]][y+dy[dd]]==-1){
                    break;
                }else{
                    d = (d+1)%4;
                    dd = (dd+1)%4;
                }
            }
            x += dx[d];
            y += dy[d];
        }
        // output
        for (int yy=1; yy<=n; yy++){
            for (int xx=1; xx<=n; xx++){
                printf(map[xx][yy]==1?"#":" ");
            }
            printf("\n");
        }
    }
    
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int nn;
  cin >> nn;
  REP(datan,nn) {
    int n;
    cin >> n;
    
    char c[n][n];
    memset(c,' ',sizeof(c));
    if (n==2) c[1][0] = '#';
    int dx[] = {0,1,0,-1};
    int dy[] = {-1,0,1,0};

    int x = 0, y = n - 1;

    c[x][y] = '#';
    int h = n-1;
    int d = 0;
    int j = 0;
    while(h>0) {
      REP(i,h) {
        x += dx[d];
        y += dy[d];
        c[x][y]=  '#';
      }

      if (h==1) break;
      d = (d+1)%4;
      if (j>=2 && j%2==0)
        h-=2;
      j++;
    }
    if (datan) cout << endl;
    REP(y,n) {
      REP(x,n) {
        cout << c[x][y];
      }
      cout << endl;
    }
  }
}