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>
using namespace std;
#define MAX_D 101


bool dx[MAX_D][MAX_D];

void resetDx();
void makeDx();
void dispDx();
bool checkField(int n);
int d;

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

void resetDx(){
    for(int i = 0; i < MAX_D; i++){
        for(int j = 0; j < MAX_D; j++){
            dx[i][j] = false;
        }
    }
}

void dispDx(){
    for(int i = 0; i < d; i++){
        for(int j = 0; j < d; j++){
            if(dx[i][j] == true) cout <<'#';
            else cout <<' ';
        }
        cout <<endl;
    }
}

void makeDx(){
    for(int i = 0; i < d; i++){
        dx[i][0] = true;
        dx[0][i] = true;
        dx[i][d - 1] = true;
    }
    int x = d - 1, y = d - 1, mx[4] = {-1, 0, 1, 0}, my[4] = {0, -1, 0, 1}, i = 0;
    bool moveFlg = false;
    while(true){
        while(checkField(x + mx[i]) && checkField(y + my[i]) && !dx[y + my[i] + my[i]][x + mx[i] + mx[i]]){
            if(!moveFlg) moveFlg = true;
            dx[y + my[i]][x + mx[i]] = true;
            x += mx[i]; y += my[i];
        }
        if(!moveFlg) break;
        //cout <<"now = " <<y <<", " <<x <<" iBefore = " <<i <<endl;
        i = (i + 1) % 4;
        int conti = (i + 2) % 4;
        int ny = y + my[i], nx = x + mx[i];
        bool endFlg = false;
        //cout <<"ny = " <<y <<"+" <<my[i] <<"=" <<ny <<", nx = " <<x <<"+" <<mx[i] <<"=" <<nx <<endl;
        for(int j = 0; j < 4; j++){
            if(j == conti) continue;
            else{
                if(checkField(ny + my[j]) && checkField(nx + mx[j])){
                    //cout <<"j = " <<j <<" ny + my[j] = " <<ny + my[j] <<", nx + mx[j] = " <<nx + mx[j];
                    if(dx[ny + my[j]][nx + mx[j]]){
                        //cout <<" true" <<endl;
                        endFlg = true;
                        break;
                    } else{
                        //cout <<" false" <<endl;
                    }
                }
            }
        }
        if(endFlg){
            break;
        }
        //dispDx();
    }
    dispDx();
}

bool checkField(int n){
    bool ret = true;
    if(n < 0 || n >= d) ret = false;
    return ret;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
const int MOD=100000;
int main(){
	int d,n;
	cin>>d;
	while(d--){
		cin>>n;
		char fld[n][n],a,b;
		REP(i,(n+1)/2){
			if(i%2==0){a='#';b=' ';}
			else{a=' ';b='#';}
			REP(j,(n-i*2)){
				fld[i+j][i]=a;
				fld[i][i+j]=a;
				fld[n-1-i][i+j]=a;
				fld[i+j][n-1-i]=a;
			}
			fld[i+1][n-1-i]=b;
		}
		REP(i,n){
			REP(j,n)cout<<fld[j][i];
			cout<<endl;
		}
		if(d!=0)cout<<endl;
	}
}

C++: #include<iostream>
#include<vector>
using namespace std;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
main()
{
	int d;cin>>d;
	while(d--)
	{
		int N;
		cin>>N;
		vector<string>ans(N,string(N,' '));
		int r=0;
		int x=N-1,y=0;
		int id=0,pre=-114514;
		while(true)
		{
			ans[x][y]='#';
			bool flag=false;
			int pr=r;
			for(int i=0;i<4;i++,r=(r+1)%4)
			{
				int tx=x+dx[r]*2,ty=y+dy[r]*2;
				if(tx>=0&&ty>=0&&tx<N&&ty<N)
				{
					if(ans[tx][ty]!='#')break;
				}
				else
				{
					tx-=dx[r];ty-=dy[r];
					if(!(tx<0||ty<0||tx>=N||ty>=N))break;
				}
				if(i==3)
				{
					flag=true;
					break;
				}
			}
			if(flag)break;
			id++;
			if(pr!=r)
			{
				if(id==pre+1)break;
				pre=id;
			}
			x+=dx[r],y+=dy[r];
		}
		for(int i=0;i<N;i++)cout<<ans[i]<<endl;
		if(d)cout<<endl;
	}
}