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>
using namespace std;

class Guruguru
{
public:
  Guruguru(int n);
  ~Guruguru();

  void Show();

private:
  char** m_guruguru;
  int m_n;
  int m_now_i;
  int m_now_j;
  int m_direction;    //  0:上 1:右 2:下 3:左

  void make_guruguru();
  void put_point();
  void move_point();
  void change_direction();
  bool check_point();
};

Guruguru::Guruguru(int n)
{
  m_n = n;
  m_guruguru = new char*[m_n];
  for (int i=0; i<m_n; i++)
  {
    m_guruguru[i] = new char[m_n];
  }

  for (int i=0; i<n; i++)
  {
    for (int j=0; j<n; j++)
    {
      m_guruguru[i][j] = ' ';
    }
  }

  m_now_i = m_n-1;
  m_now_j = 0;
  m_direction = 0;
  make_guruguru();
}

Guruguru::~Guruguru()
{
  for (int i=0; i<m_n; i++)
  {
    delete[] m_guruguru[i];
  }
  delete[] m_guruguru;
}

void Guruguru::make_guruguru()
{
  int num = 0;
  while(true)
  {
    put_point();
    if (! check_point() )
    {
      if ( num == m_n-1 ) return;
      change_direction();
      num++;
    }
    move_point();
  }
}

void Guruguru::put_point()
{
  m_guruguru[m_now_i][m_now_j] = '#';
}

void Guruguru::move_point()
{
  switch(m_direction)
  {
  case 0:
    m_now_i--;
    break;
  case 1:
    m_now_j++;
    break;
  case 2:
    m_now_i++;
    break;
  case 3:
    m_now_j--;
    break;
  }
}

void Guruguru::change_direction()
{
  switch(m_direction)
  {
  case 3:
    m_direction = 0;
    break;
  default:
    m_direction++;
    break;
  }
}

bool Guruguru::check_point()
{
  int next_i = m_now_i;
  int next_j = m_now_j;
  int next_next_i = next_i;
  int next_next_j = next_j;
  switch(m_direction)
  {
  case 0:
    next_i--;
    next_next_i-=2;
    break;
  case 1:
    next_j++;
    next_next_j+=2;
    break;
  case 2:
    next_i++;
    next_next_i+=2;
    break;
  case 3:
    next_j--;
    next_next_j-=2;
    break;
  }
  if ( next_i < 0 || m_n <= next_i || next_j < 0 || m_n <= next_j )
  {
    return false;
  }
  else if ( next_next_i < 0 || m_n <= next_next_i || next_next_j < 0 || m_n <= next_next_j )
  {
    return true;
  }
  else
  {
    if ( '#' == m_guruguru[next_next_i][next_next_j] )
    {
      return false;
    }
    return true;
  }
}

void Guruguru::Show()
{
  for (int i=0; i<m_n; i++)
  {
    for (int j=0; j<m_n; j++)
    {
      cout << m_guruguru[i][j] << flush;
    }
    cout << endl;
  }
}

int main()
{
  int d;  //  データセットの個数
  cin >> d;
  for (int dataset=0; dataset<d; dataset++)
  {
    int n;  //  辺の長さ
    cin >> n;
    Guruguru guruguru(n);

    guruguru.Show();
    if (! dataset == d-1) cout << endl;
  }
}

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

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

char f[100][100];
int dh[4] = {-1, 0, 1, 0};
int dw[4] = {0, 1, 0, -1};
int main(void){
	int d, n, i, j;
	scanf("%d", &d);
	for(;d--;){
		scanf("%d", &n);
		rep(i, n)rep(j, n){
			f[i][j] = ' ';
			if(i == 0 || j == 0 || i == n - 1 || j == n - 1)f[i][j] = '#';
		}
		
		int w = 2, h = n - 1, k = 0;
		f[h][w - 1] = ' ';
		if(n >= 4)for(int times = 200000;times--;){
			//printf("%d %d\n", h, w);
			f[h][w] = '#';
			if(f[h + dh[k] + dh[k]][w + dw[k] + dw[k]] == '#')k = (k + 1) % 4;
			else if(f[h + dh[k] + dh[(k + 1) % 4]][w + dw[k] + dw[(k + 1) % 4]] == '#')k = (k + 1) % 4;
			else if(f[h + dh[k] + dh[(k + 3) % 4]][w + dw[k] + dw[(k + 3) % 4]] == '#')k = (k + 1) % 4;
			else{ h += dh[k]; w += dw[k]; }
		}

		rep(i, n){
			rep(j, n)printf("%c",f[i][j]);
			puts("");
		}
		if(d >= 0)puts("");
	}
	return 0;
}

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(;;){
      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;
    }
    for(int i=0;i<n;++i){
      for(int j=0;j<n;++j)cout<<((field[i][j])?'#':' ');
      cout<<endl;
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;

int sp[101][101];

main(){
	int e;
	cin>>e;
	int flag=0;
	while(e--){
		if(flag)puts("");
		++flag;
		int n;
		cin>>n;
		if(n==1){
			cout<<"#"<<endl;
			continue;
		}
		memset(sp,0,sizeof(sp));
		
		int y=n-1,x=0,d=0;
		int c=0;
		while(true){
			
			
			if(d==0){
				if(sp[y-1][x+1]==1)break;
				int cy=y;
				while(cy>=1){
					if(cy>=2 && sp[cy-2][x]==1)break;
					--cy;
				}
				if(y==cy)break;
				d=(d+1)%4;
				for(;cy<=y;y--)sp[y][x]=1;
				++y;
				continue;
			}
			
			if(d==1){
				if(sp[y+1][x+1]==1)break;
				int cx=x;
				while(cx<=n-2){
					if(cx<=n-3 && sp[y][cx+2]==1)break;
					++cx;
				}
				if(x==cx)break;
				d=(d+1)%4;
				for(;cx>=x;x++)sp[y][x]=1;
				--x;
				continue;
			}
			
			if(d==2){
				if(sp[y+1][x-1]==1)break;
				int cy=y;;
				while(cy<=n-2 && !(cy<=n-3 && sp[cy+2][x]==1))++cy;
				if(cy==y)break;
				d=(d+1)%4;
				for(;cy>=y;y++)sp[y][x]=1;
				--y;
				continue;
			}
			
			if(d==3){
				if(sp[y-1][x-1]==1)break;
				int cx=x;
				while(cx>=1 && !(cx>=2 && sp[y][cx-2]==1))--cx;
				if(x==cx)break;
				d=(d+1)%4;
				for(;cx<=x;x--)sp[y][x]=1;
				++x;
				continue;
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(sp[i][j])printf("#");
				else printf(" ");
			}
			puts("");
		}

	}
}