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>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());

int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int main(){
	int n;
	cin>>n;
	rep(l,n){
		int m;
		cin>>m;
		cout<<m<<endl;
		if(m==1){
			cout<<"#\n\n";
			continue;
		}
		if(m==2){
			cout<<"##\n";
			cout<<"# \n\n";
			continue;
		}
		if(m==3){
			/*
			###
			# #
			# #
			*/
			cout<<"###\n";
			cout<<"# #\n";
			cout<<"# #\n";
			cout<<"\n";
			continue;
		}
		vector<vector<char> > vv;
		initvv(vv,m,m);
		rep(i,m){
			rep(j,m) vv[i][j]=' ';
		}
		rep(i,m){
			vv[0][i]='#';
			vv[m-1][i]='#';
			vv[i][m-1]='#';
			vv[i][0]='#';
		}
		vv[m-1][1]=' ';
		int x=m-1,y=2;
		int c=0;
		while(1){
			// cout<<x<<" "<<y<<endl;
			if(vv[x+dx[c]*2][y+dy[c]*2]==' '){
				x+=dx[c];
				y+=dy[c];
				vv[x][y]='#';
			}
			else if(vv[x+2*dx[(c+1)%4]][y+2*dy[(c+1)%4]]==' '&&vv[x+dx[(c+1)%4+dx[(c+2)%4]]][y+dy[(c+1)%4]+dy[(c+2)%4]]==' '){
				c++;
				c%=4;
				x+=dx[c];
				y+=dy[c];
				vv[x][y]='#';
			}
			else{
				break;
			}
		}
		/*
		######
		#    #
		# ## #
		# #  #
		# #  #
		# ####
		*/
		rep(i,m){
			rep(j,m){
				cout<<vv[i][j];
			}
			cout<<endl;
		}
		cout<<endl;
	}
}

C++: import java.util.Scanner;


public class Main {

	char[][] makeSpiral(int n){
		char[][] pattern=new char[n][n];
		int dir=0;
		int x=0,y=n-n%2;
		int[] dx={0,1,0,-1};
		int[] dy={-1,0,1,0};
		for(int i=0;i<n;++i){
			for(int j=0;j<n;++j){
				pattern[i][j]=' ';
			}
		}
		int notTurn=2;
		boolean end=false;
		while(!end){

			if(n%2==0||notTurn<2){
				x+=dx[dir];
				y+=dy[dir];
			}
			--notTurn;
			
			switch(dir){
			case 0:
				if(y<=0||(y!=1&&pattern[x][y-2]=='*')){
					if(notTurn>0){
						end=true;
					}
					else{
						dir=1;
						notTurn=2;
					}
				}
				break;
			case 1:
				if(x>=n-1||(x!=n-2&&pattern[x+2][y]=='*')){
					if(notTurn>0){
						end=true;
					}
					else{
						dir=2;
						notTurn=2;
					}
				}
				break;
			case 2:
				if(y>=n-1||(y!=n-2&&pattern[x][y+2]=='*')){
					if(notTurn>0){
						end=true;
					}
					else{
						dir=3;
						notTurn=2;
					}
				}
				break;
			case 3:
				if(x<=0||(x!=1&&pattern[x-2][y]=='*')){
					if(notTurn>0){
						end=true;
					}
					else{
						dir=0;
						notTurn=2;
					}
				}
				break;
			}
			pattern[x][y]='*';
		}
		return pattern;
	}
	
	void io(){
		Scanner s=new Scanner(System.in);
		int setnum=s.nextInt();
		for(int i=0;i<setnum;++i){
			int n=s.nextInt();
			char[][] pattern=makeSpiral(n);
			
			if(i>0)System.out.println();
			for(int y=0;y<n;++y){
				for(int x=0;x<n;++x){
					System.out.print(pattern[x][y]);
				}
				System.out.println();
			}
		}
		s.close();
	}

	public static void main(String[] args) {
		new Main().io();
	}

}

C++: #include<vector>
#include<map>
#include<climits>
#include<set>
#include<queue>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<string>
#include<bitset>
#include<cstring>
#include<list>
using namespace std;
typedef vector<int>VI;
typedef vector<VI>VVI;
typedef vector<string>VS;
typedef pair<int,int>PII;
typedef long long LL;
typedef pair<LL,LL>PLL;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define PB push_back
#define EACH(i,c) for(typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define F first
#define S second

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

char fld[200][200];
int N;
void init(){
    fill(*fld,*fld+40000,' ');
    REP(i,N+4)fld[0][i]=fld[N+3][i]=fld[i][0]=fld[i][N+3]='#';
}

void solve(){
    int x=2,y=N+1,d=1;
    while(true){
        fld[y][x]='#';
        bool f2=false;
        REP(i,4){
            int nd=(d+i)%4;
            int ny=y+dy[nd];
            int nx=x+dx[nd];
            bool flag=true;
            REP(j,4){
                if(j==(nd+2)%4)continue;
                if(fld[ny+dy[j]][nx+dx[j]]=='#')flag=false;
            }
            if(flag){
                x=nx;
                y=ny;
                d=nd;
                f2=true;
                break;
            }

        }
        if(f2==false)return;
    }
}


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

    while(n--){
        cin>>N;
        init();
        solve();
        if(N==2){
            cout<<"##"<<endl;
            cout<<"#"<<endl;
            continue;
        }
        FOR(i,2,N+2){
            FOR(j,2,N+2)cout<<fld[i][j];
            cout<<endl;
        }
    }

    return 0;
}