首页 > 代码库 > 用C++实现顺时针增加的螺旋队列

用C++实现顺时针增加的螺旋队列

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAATgAAAB6CAYAAAArmE+5AAAN/ElEQVR4Ae1d244cSRHtrmmPDV/lv4AH1iwIcZGQgL18B2/wCyDxC4AFL0hYWp7gGxDSwopdGUarbSp6fMrRMZGXamY8FZGnpHZmxi3jnMjMqZ6pbu//+Ic/HX/3+9/ufv2r3+x4kQEyQAYyMXD42tefnQ63v/7tk0y4iIUMkAEysJt2R7JABsgAGcjJwJQTFlGRATJABnbzHdyeNJABMkAGcjJw8GC9fPly9/z5c0+VWia49TUaB6Pj17UHF6OsAeDVHEg/Ov47B1wJqAWebQzcKKiM5YVxNrwWz+j4NR/gQstG6Wdb78sBN3JRR1m8NZzZFnYNa03HfVBjJ55u+SODLPCRF/no+OMt3fvPGIfbyPvg/ll93IjLHdzjprGt2Ude6MAuFRlpowP3SJi9XQcestR//+rVX44v3vvODg/6AuCohR4dPxb9aDwAL/DrdsS9AD6iY+cdnFrJWYqqILHbyYDdyFwLncRt3Gz5HdzG83zw9OyCljFkDz75BiYYDe8GKN9UClnrv7xF/cUvf+4Sbn+yuUYJhKXDbBT8UkLLwUjY9RIelYeMuPev/vzJ8cWL7y6/g9OFZp8MkAEyEJkBvkWNXD3mTgbIQJUBHnBVeqgkA2QgMgM84CJXj7mTATJQZYAHXJUeKskAGYjMwHzA7Xd7fmVS5BoydzJABgoMTPv5dJMXLzJABshANgYOcrhN0/k7VXkepvQMlH5WpmQTnSQPv8Zt8WXjwcMvmC0H2XCjriPj1zXOUN+DHG5XV1eo7Z1FvCjUAs8AXOPSfV1gLUdfY2/ZwidSW8IEOfDLWF4YR8JYyxU4rQ3kwCtjeWFs7SOOLcaIGGzOBzncrg9PTsWySj3OCN7Dp2Uj9VHfkTBrrMT/8kRHpgNbAE2Hq8Pu+vr69JMoG7hTxTr/Eew1/C195zSbNevFl/Ug6MW/2QIyMZeBw5Mnh92zp09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iu2eGk9YkBmx1ru+UMvrfW1Y23LPr8LjWtgd3TfotbuPOZNVbwz6dVJ/JrtXJjTJTb2sjLkWosHGxurNLb5YVyzh403l83ZjsWn5i/zevhaPqV8KScDwzBg7+Bm4Mvdm/TnjXU2LsngZ+1lbGUl25Yc+p4W85banhhbsEH+pVxK3JbsKT9f3+QjOR835i0qC5684OYHGOvNemdeA9NXX8kNGi8yQAbIQD4GppvXN/lQEREZIANkYGZg+vLmSxJBBsgAGUjJwHTEp+1TwiMoMkAGRmZgGhk8sZMBMpCbAR5wuetLdGRgaAZ4wA1dfoInA7kZ4AGXu75ERwaGZmA6PeY4NAUETwbIQFYGeAeXtbLERQbIwG7i5xi4CsgAGcjKAO/gslaWuMgAGdjxgOMiIANkIC0DPODSlpbAyAAZ4AHHNUAGyEBaBnjApS0tgZEBMsADjmuADJCBtAzwgEtbWgIjA2SABxzXABkgA2kZ4AGXtrQERgbIAA84rgEyQAbSMjAfcPywVtrqEhgZGJwB3sENvgAInwxkZoAHXObqEhsZGJyB/wEIQ+JKXwTBcAAAAABJRU5ErkJggg==" alt="" />
#include<iostream>
using namespace std;
int a[10][10];
void foo(int n)
{
	int m=1;
	int i,j;
	for(i=0;i<n/2;++i)
	{
		for(j=0;j<n-i;++j)
		{
			if(a[i][j]==0)
				a[i][j]=m++;
		}
		for(j=i+1;j<n-i;++j)
		{
			if(a[j][n-i-1]==0)
				a[j][n-i-1]=m++;
		}
		for(j=n-i-1;j>i;j--)
		{
			if(a[n-i-1][j]==0)
			{
				a[n-i-1][j]=m++;
			}
		}
		for(j=n-i-1;j>i;--j)
		{
			if(a[j][i]==0)
				a[j][i]=m++;
		}
	}

}
int main()
{
	for(int i=0;i<5;++i)
    {
		for(int j=0;j<5;++j)
			a[i][j]=0;

    }
	foo(5);
	for(int i=0;i<5;++i)
    {
		for(int j=0;j<5;++j)
			cout<<a[i][j]<<'\t';
		cout<<endl;
    }
    system("pause");
	return 0;
}

用C++实现顺时针增加的螺旋队列