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
		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<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(),n.end()
#define pb push_back
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define INF (1<<27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const int dx[4] = {-1,0,+1,0};
const int dy[4] = {0,-1,0,+1};

class Solve{
private:
  int d;
  int imap[110][110];
public:

  Solve(int n):d(n){}
  ~Solve(){}

  int Connect(P p){
    int cnt = 0;
    rep(i,4){
      int nx = p.F+dx[i],ny = p.S+dy[i];
      if(!(0<=nx && nx <d && 0<= ny && ny < d))continue;
      if(imap[ny][nx] == 1)cnt++;
    }
    return cnt;
  }


  void display(){
      cout << "--- in display ---" << endl;
      rep(i,d){
	rep(j,d){
	  if(imap[i][j] == 1)cout << '#';
	  else cout << ' ';
	}
	cout << endl;
      }
      cout << endl;
    }


  void run(){
    rep(i,d+1)rep(j,d+1)imap[i][j] = 0;

    rep(i,d){
      imap[0][i] = 1;
      imap[i][0] = 1;
      imap[i][d-1] = 1;
    }

  


    P pos = P(d-1,d-1);
    int dir = 0;
    int cnt = 0;
    bool fin = false;
    while(true){

      int nx,ny;
      nx  = pos.F,ny = pos.S;
      while(true){
	//cout << "nx = " << nx << ",ny = "<< ny << ",dir = " << dir << endl;
	//display();


	if(!(Connect(P(nx,ny)) == 2 || Connect(P(nx,ny)) == 1)){
	  imap[ny-dy[dir]][nx-dx[dir]] = 0;
	  //cout << "Lase nx = " << nx << ",ny = "<< ny << endl;
	  //cout << "FIN! connnect = " << Connect(P(nx,ny)) << endl;
	  fin = true;
	  break;
	}

	if(imap[ny+dy[dir]][nx+dx[dir]] == 1){
	  //cout << "IN nx = " << nx+dx[dir] << ",ny = " << ny+dy[dir] << endl;
	  imap[ny][nx] = 0;
	  cnt--;
	  break;
	}
	imap[ny][nx] = 1;
	cnt++;	


	nx += dx[dir],ny += dy[dir];
      }
      //cout << "------ cnt = " << cnt << endl;
      if(fin){
	//cout << "dir = " << dir << endl;
	break;
      }
      if(cnt == 0)break;
      cnt =0;

      pos.F = nx-dx[dir],pos.S = ny-dy[dir];
      dir++;
      dir = dir%4;
    }

    rep(i,d){
      rep(j,d){
	if(imap[i][j] == 1)cout << '#';
	else cout << ' ';
      }
      cout << endl;
    }
cout << endl;

  }


};


int main(){
  int T;
  cin >> T;
  while(T-- > 0){
    int _n;
    cin >> _n;
    Solve ans = Solve(_n);
    ans.run();
  }
return 0;
}

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

using namespace std;

int main() {
	int n;
	cin >> n;
	for (int j = 0; j < n; j++) {
		int a;
		cin >> a;
		vector< vector<int> > uzu(a, vector<int>(a, 0));
		int ulimit, dlimit, rlimit, llimit;
		ulimit = 0;
		dlimit = a-1;
		rlimit = a-1;
		llimit = 2;
		int tate, yoko;
		tate = a-1;
		yoko = 0;
		while (1) {
			if (yoko > rlimit || tate == ulimit) {
				break;
			}
			for (int i = tate; i >= ulimit; i--) {
				uzu[i][yoko] = 1;
			}
			tate = ulimit;
			ulimit += 2;
			if (tate > dlimit || yoko == rlimit) {
				break;
			}
			for (int i = yoko; i <= rlimit; i++) {
				uzu[tate][i] = 1;
			}
			yoko = rlimit;
			rlimit -= 2;
			if (yoko < llimit || tate == dlimit) {
				break;
			}
			for (int i = tate; i <= dlimit; i++) {
				uzu[i][yoko] = 1;
			}
			tate = dlimit;
			dlimit -= 2;
			if (tate < ulimit || yoko == llimit) {
				break;
			}
			for (int i = yoko; i >= llimit; i--) {
				uzu[tate][i] = 1;
			}
			yoko = llimit;
			llimit += 2;
		}
		for (int i = 0; i < a; i++) {
			for (int k = 0; k < a; k++) {
				if (uzu[i][k] == 1) {
					cout << '#';
				} else {
					cout << ' ';
				}
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include<stdio.h>
#define N 500
int main(void)
{
  int v[N][N]={};
  int line=0;
  int line2=0;
  int i=0;
  int j=0;
  int a=1;
  int n=0;
  int k=0;

  scanf("%d", &n);
  while(k++<n)
    {
      scanf("%d", &line);

      line2=line;

      for (i=0;i<line;i++)
        {
          v[line][i]=2;
          v[0][i]=1;
        }

      while (line>=3)
        {
          for (i=2*(a-1);i<line;i++)
            {
              v[i][2*(a-1)]=1;
              v[line-1][i]=1;
            }

          for (;i>2*a+1;i--)
            {
              v[i-1][line-1]=1;
              v[2*a][i-1]=1;
            }

          a++;
          line-=2;
        }
      if((line2-6)%4==0)
        v[line2-2*(line2-2)/4-1][line2-2*(line2-2)/4-1]=0;

      for (j=0;j<line2;j++)
        for (i=0;i<=line2;i++)
          {
            switch (v[i][j])
              {
              case 0 : printf(" "); break;
              case 1 : printf("#"); break;
              case 2 : printf("\n"); break;
              }
            v[i][j]=0;
          }

      a=1;

    }

  return 0;
}

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

using namespace std;

main(){
  int n;
  char data[105][105];
  int dx[] = {0, 1, 0, -1};
  int dy[] = {-1, 0, 1, 0};
  int t;
  cin >> t;
  for(int tc=0;tc<t;tc++){
    cin >> n;
    if(tc != 0) cout << endl;
    if(n == 1){
      cout << "#" << endl;
      continue;
    }
    int y = n-1;
    int x = 0;
    fill(data[0], data[100], ' ');
    data[y][x] = '#';
    int d = 0;
    while(1){
      int ty = y + dy[d];
      int tx = x + dx[d];
      if(ty<0 || ty>=n){ d = (d+1)%4; continue; }
      if(tx<0 || tx>=n){ d = (d+1)%4; continue; }
      if(data[ty][tx] == '#') break;
      int tty = ty + dy[d];
      int ttx = tx + dx[d];
      if((tty>=0 || tty<n) && (ttx>=0 || ttx<n)){
	if(data[tty][ttx] == '#'){
	  d = (d+1)%4; 
	  continue; 
	}
      }
      int cnt = 0;
      for(int i=0;i<4;i++){
	int tty = ty + dy[i];
	int ttx = tx + dx[i];
	if(tty<0 || tty>=n) continue;
	if(ttx<0 || ttx>=n) continue;
	if(data[tty][ttx] == '#') cnt++;
      }
      if(cnt > 1) break;
      data[ty][tx] = '#';
      y = ty;
      x = tx;
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cout << data[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}