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.Printf("##\n# \n")
			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 <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int n;
int fie[110][110]={};
int d[]={-1,0,1,0,-1};


bool turn(int dir,int y,int x){
    int tmpy=y+d[dir];
    int tmpx=x+d[dir+1];
//    cout<<endl;
//    dump(dir);dump(y);dump(x);dump(tmpy);dump(tmpx);
    
    if(fie[y][x]==1 || tmpy<0 || tmpy>=n || tmpx<0 || tmpx>=n || fie[ tmpy+d[dir] ][ tmpx+d[dir+1] ]==1 || fie[ tmpy+d[dir+1] ][ tmpx+d[(dir+1)%4+1] ]==1 )return true;
    else return false;
}


int main() {
    
    int aa;
    cin>>aa;
    rep(i,aa){
        rep(a,110) rep(b,110) fie[a][b]=0;
        cin>>n;
        
        int dir=0;
        int y=n-1,x=0;
        
        int c=0;
        while(1){
            if( turn(dir,y,x) ){
                c++;
                dir++;
                dir%=4;
            }
            else{
                c=0;
                fie[y][x]=1;
                y+=d[dir];
                x+=d[dir+1];
            }
            if(c==4){
                fie[y][x]=1;
                break;

            }
        }
        
        rep(i,n){
            rep(j,n){
                if(fie[i][j]==1)cout<<"#";
                else cout<<" ";
            }
            cout<<endl;
        }
    }
    
    return 0;
}

C++: #include <iostream>
#include <cstring>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

void solve(int n) {
	char field[n][n + 1];
	memset(field, ' ', sizeof(field));
	for (int i = 0; i < n; i++) {
		field[i][n] = '\0';
	}
	int x = 0, y = n - 1, d = 0;
	while (true) {
		field[y][x] = '#';
		int newX = x + dx[d];
		int newY = y + dy[d];
		if (newX < 0 || n <= newX || newY < 0 || n <= newY
				|| field[newY+ dy[d]][newX+ dx[d]] == '#') {
			d = (d + 1) % 4;
			newX = x + dx[d];
			newY = y + dy[d];
		}
		int count = 0;
		for (int i = 0; i < 4; i++) {
			int cx = newX + dx[i];
			int cy = newY + dy[i];
			if (0 <= cx && cx < n && 0 <= cy && cy < n && field[cy][cx] == '#') count++;
		}
		if (count > 1) break;
		x = newX;
		y = newY;
	}
	for (int i = 0; i < n; i++) {
		cout << field[i] << endl;
	}
}

int main () {
	int d;
	cin >> d;
	for (int i = 0; i < d; i++) {
		int n;
		cin >> n;
		solve(n);
	}
	return 0;
}

C++: // 2011/03/15 Tazoe

#include <iostream>
using namespace std;

int main()
{
	int d;
	cin >> d;

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

		char spr[100][100];
		for(int j=0; j<n; j++)
			for(int k=0; k<n; k++)
				spr[j][k] = ' ';

		int a = 0;
		int b = n+1;
		for(int j=b-2; j>=a; j--)
			spr[j][a] = '#';
		while(true){
			b -= 2;
			if(a>b)
				break;

			for(int k=a; k<=b; k++)
				spr[a][k] = '#';

			if(b-a<=1)
				break;

			for(int j=a; j<=b; j++)
				spr[j][b] = '#';

			a += 2;
			if(a>b)
				break;

			for(int k=b; k>=a; k--)
				spr[b][k] = '#';

			if(b-a<=1)
				break;

			for(int j=b; j>=a; j--)
				spr[j][a] = '#';

		}

		if(i!=0)
			cout << endl;
		for(int j=0; j<n; j++){
			for(int k=0; k<n; k++){
				cout << spr[j][k];
			}
			cout << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <cstdlib>
const static int SIZE = 100;
const static int MARGIN = 2;

const static int F_NONE = 0;
const static int F_WALL = 1;
const static int F_LINE = 2;

const static int DIR_N = 4;

using namespace std;


void draw( int n ) {
  int field[ SIZE + MARGIN * 2 ][ SIZE + MARGIN * 2 ] = { 0 };
  int pos_y = n + MARGIN - 1;
  int pos_x = MARGIN;
  int vy[] = { -1, 0, 1,  0 };
  int vx[] = {  0, 1, 0, -1 };
  int dir = 0;
  int lastMoveDir = -1;
  int turnNum = 0;
  for( int i = 0; i < n + MARGIN * 2; i++ ) {
    field[ i ][ 0 ] = F_WALL;
    field[ 0 ][ i ] = F_WALL;
    field[ i ][ n + MARGIN * 2 - 1 ] = F_WALL;
    field[ n + MARGIN * 2 - 1 ][ i ] = F_WALL;
  }

  while( 1 ) {
    field[ pos_y ][ pos_x ] = F_LINE;
    if(
      field[ pos_y + vy[ dir ] * 2 ][ pos_x + vx[ dir ] * 2 ] == F_NONE 
    ) {
      pos_y += vy[ dir ];
      pos_x += vx[ dir ];
      lastMoveDir = dir;
    } else {
      dir = ( dir + 1 ) % DIR_N;
      if( turnNum++ == n - 1 ) { break; }
    }
  }

  for( int i = MARGIN; i < n + MARGIN; i++ ) {
    for( int j = MARGIN; j < n + MARGIN; j++ ) {
      cout << ( field[ i ][ j ] ? '#' : ' ' ) << flush;
    }
    cout << endl;
  }
}


int main() {
  int d;
  cin >> d;
  for( int i = 0; i < d; i++ ) {
    int n;
    cin >> n;
    draw( n );
    if( i != d - 1 ) {
      cout << endl;
    }
  }
  return EXIT_SUCCESS;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int main(void){
  int d;cin>>d;
  const int POS[][2]={{0,-1},{1,0},{0,1},{-1,0}};
  while(d--){
    int n;cin>>n;
    vector<vector<char> > field(n+2);
    for(int i=0;i<=n+1;++i){
      field[i].reserve(n+2);
      for(int j=0;j<=n+1;++j)field[i][j]=(i==n+1||j==n+1)?1:0;
    }
    int x=0,y=n-1,p=0;
    for(;;){
    	if(x<0||y<0)break;
      int dx=POS[p][0],dy=POS[p][1];
      int px=POS[(p+4+1)%4][0],py=POS[(p+4+1)%4][1],qx=POS[(p+4-1)%4][0],qy=POS[(p+4-1)%4][1];
      field[y][x]=1;
      x+=dx,y+=dy;
      if(y==0&&x==0||(y+2*dy>=0&&x+2*dx>=0&&field[y+2*dy][x+2*dx]==1))p=(p+1)%4;
      if((y+dy+py>=0&&x+dx+px>=0&&field[y+dy+py][x+dx+px]==1)||(y+dy+qy>=0&&x+dx+qx>=0&&field[y+dy+qy][x+dx+qx]==1))
      	break;
    }
    if(n==2)field[1][1]=0;
    for(int i=0;i<n;++i){
      for(int j=0;j<n;++j)cout<<((field[i][j])?'#':' ');
      cout<<endl;
    }
    if(d)cout<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;


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

int main(){
	bool fst=true;
	int n;
	cin>>n;
	while(n--){
		memset(a,' ',sizeof(a));
		int m,y,x,dir;
		cin>>m;
		if(m==1){
			if(!fst)cout<<endl;		
			cout<<"#\n";
			fst=false;
			continue;
		}
		y=m;x=1;dir=0;
		a[y][x]='#';
		while(1){
			for(int i=0;i<m-1;i++){
				if(a[y+2*dy[dir]][x+2*dx[dir]]=='#')break;
				y+=dy[dir];
				x+=dx[dir];
				a[y][x]='#';
			}
			dir=(dir+1)%4;
			if(a[y+2*dy[dir]][x+2*dx[dir]]=='#')break;
			if(a[y-1][x-1]=='#'||a[y-1][x+1]=='#'||a[y+1][x-1]=='#'||a[y+1][x+1]=='#')break;
		}
		if(!fst)cout<<endl;
		fst=false;	
		for(int i=1;i<=m;i++){
			for(int j=1;j<=m;j++)cout<<a[i][j];
			cout<<endl;
		}
	}
	return 0;
}