天天看點

螺旋隊列

// route.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define min(x,y) ((x) < (y) ?: (x),(y))
void fun(int n);
#include <iostream>
using namespace std;


int main(int argc, char* argv[])
{
	printf("Hello World!\n");
	fun(10);
	return 0;
}

void fun(int n)
{
	int j;
	int** a = (int**)malloc(n*sizeof(int*));
	for(int i = 0; i < n; i++)
	{
		*(a+i) = (int*)malloc(n*sizeof(int));
	}
	int m =1;

	for(i = 0; i < n/2; i++)
	{
		for(j = i; j < (n - i); j++)
			a[i][j] = m++;
		for(j = i+1; j < (n - i - 1); j++)
			a[j][n -i -1] = m++;
		for(j = n-i-1; j >= i; j--)
			a[n-i-1][j] = m++;
		for(j = n-i-2; j > i; j--)
			a[j][i] = m++;
	}
	for(i=0; i<n;i++)
	{
		for(j=0; j<n; j++)
		{
			cout<<a[i][j]<<"\t";
		}
		cout<<endl;
	}
	for(i=0; i<n; i++)
	{
		free(a[i]);
	}
	free(a);
}
           
螺旋隊列