陀罗输出

#include <iostream>
#include <iomanip>
using std::endl;
using std::cout;
using std::setw;

void OutputTuoluo(const int kSize)
{
	int *array_data = new int[kSize*kSize];
	int right = kSize;     
	int down = kSize - 1;
	int left = kSize - 1;
	int up   = kSize - 2;
	int total_num = 0;
	int m = -1;
	int kTotalNum = kSize * kSize;
	while(total_num < kTotalNum)
	{
		//c从左到右
		for(int temp_right = right; temp_right > 0; --temp_right)
		{
			array_data[++m] = (++total_num);
		}
		//从上到下	
		for(int temp_down = down; temp_down > 0; --temp_down)
		{
			m += kSize;
			array_data[m] = (++total_num);
		}
		//从右到左
		for(int temp_left = left;temp_left > 0; --temp_left)
		{
			array_data[--m] = (++total_num);
		}
		//从下到上
		for(int temp_up = up; temp_up > 0; --temp_up)
		{
			m -= kSize;
			array_data[m] = (++total_num);
		}
		right -= 2;
		down  -= 2;
		left  -= 2;
		up    -= 2;
	}
	int index =0;
	for( m = 0; m < kSize; ++m)
	{
		for(int n = 0; n < kSize; ++n)
		{
			cout<<setw(6)<<array_data[index++];
		}
		cout<<endl<<endl;
	}
	delete []array_data;
}

int main()
{
	OutputTuoluo(1);
	OutputTuoluo(2);
	OutputTuoluo(3);
	OutputTuoluo(4);
	OutputTuoluo(5);
	OutputTuoluo(6);
	OutputTuoluo(7);
	return 0;
}

结果如下

     1


     1     2


     4     3


     1     2     3


     8     9     4


     7     6     5


     1     2     3     4


    12    13    14     5


    11    16    15     6


    10     9     8     7


     1     2     3     4     5


    16    17    18    19     6


    15    24    25    20     7


    14    23    22    21     8


    13    12    11    10     9


     1     2     3     4     5     6


    20    21    22    23    24     7


    19    32    33    34    25     8


    18    31    36    35    26     9


    17    30    29    28    27    10


    16    15    14    13    12    11


     1     2     3     4     5     6     7


    24    25    26    27    28    29     8


    23    40    41    42    43    30     9


    22    39    48    49    44    31    10


    21    38    47    46    45    32    11


    20    37    36    35    34    33    12


    19    18    17    16    15    14    13

猜你喜欢

转载自blog.csdn.net/dyingfair/article/details/79596681