# c++

lanowen 2008-03-20 12:22:24
//回旋矩阵 ??modify
/*eg: 1 2 3 1 2 3 4
8 9 4 12 13 14 5
7 6 5 11 16 15 6
10 9 8 7 */
#include <iostream>
using namespace std;
const max=100;
int a[max][max]={0};
int s=2;
//int i=n-1;
int j=0;
// int flag=1;
void main()
{
int n;
cout<<"请输入你要求的回旋矩阵的阶数："<<endl;
cin>>n;
a[n-1][0]=1;
int i=n-1;
//int k;
//int j=0;
//int flag=1;*/
int flag=1;
int rightrecycle(int &i,int n);
int downrecycle(int &i,int n);
int leftrecycle(int &i,int n);
int uprecycle(int &i,int n);
while(s!=n*n)
{
if(flag==1)
{ rightrecycle(i,n);
flag=2;}
else if(flag==2)
{ downrecycle(i,n);flag=3;}
else if(flag==3)
{ leftrecycle(i,n);flag=4;}
else
{uprecycle(i,n);flag=1;}
}
cout<<"你所求的"<<n<<"阶矩阵是"<<endl;
for( i=n-1;i>=0;i--)
{ cout<<endl;
for( j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
}
}

int rightrecycle(int &i,int n)
{
int flag=1;
while(flag)
{
a[i][j+1]=s;
j=j+1;
s++;
if(a[i][j+1]!=0 || j+1==n)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (2);
}

int downrecycle(int &i,int n)
{int flag=1;
while(flag)
{
a[i-1][j]=s;
i=i-1;
s++;
if(a[i-1][j]!=0 || i-1<0)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (3);
}

int leftrecycle(int &i,int n)
{int flag=1;
while(flag)
{
a[i][j-1]=s;
j=j-1;
s++;
if(a[i][j-1]!=0 || j-1<0)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (4);
}

int uprecycle(int &i,int n)
{ int flag=1;
while(flag)
{
a[i+1][j]=s;
j=j+1;
s++;
if(a[i+1][j]!=0 || i+1==n)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (1);
}
...全文
85 6 打赏 收藏 举报

6 条回复

• 打赏
• 举报

xkyx_cn 2008-03-20
• 打赏
• 举报

mark
aini10011 2008-03-20
• 打赏
• 举报

?_?

lanowen 2008-03-20
• 打赏
• 举报

//求回旋矩阵 请求modify
/*eg: 1 2 3 1 2 3 4
8 9 4 12 13 14 5
7 6 5 11 16 15 6
10 9 8 7 */
#include <iostream>
using namespace std;
const max=100;
int a[max][max]={0};
int s=2;
//int i=n-1;
int j=0;
// int flag=1;
void main()
{
int n;
cout < <"请输入你要求的回旋矩阵的阶数：" < <endl;
cin> > n;
a[n-1][0]=1;
int i=n-1;
//int k;
//int j=0;
//int flag=1;*/
int flag=1;
int rightrecycle(int &i,int n);
int downrecycle(int &i,int n);
int leftrecycle(int &i,int n);
int uprecycle(int &i,int n);
while(s!=n*n)
{
if(flag==1)
{ rightrecycle(i,n);
flag=2;}
else if(flag==2)
{ downrecycle(i,n);flag=3;}
else if(flag==3)
{ leftrecycle(i,n);flag=4;}
else
{uprecycle(i,n);flag=1;}
}
cout < <"你所求的" < <n < <"阶矩阵是" < <endl;
for( i=n-1;i> =0;i--)
{ cout < <endl;
for( j=0;j <n;j++)
{
cout < <a[i][j] < <" ";
}
}
}

int rightrecycle(int &i,int n)
{
int flag=1;
while(flag)
{
a[i][j+1]=s;
j=j+1;
s++;
if(a[i][j+1]!=0 ¦ ¦ j+1==n)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (2);
}

int downrecycle(int &i,int n)
{int flag=1;
while(flag)
{
a[i-1][j]=s;
i=i-1;
s++;
if(a[i-1][j]!=0 ¦ ¦ i-1 <0)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (3);
}

int leftrecycle(int &i,int n)
{int flag=1;
while(flag)
{
a[i][j-1]=s;
j=j-1;
s++;
if(a[i][j-1]!=0 ¦ ¦ j-1 <0)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (4);
}

int uprecycle(int &i,int n)
{ int flag=1;
while(flag)
{
a[i+1][j]=s;
j=j+1;
s++;
if(a[i+1][j]!=0 ¦ ¦ i+1==n)
{
//a[i][j]=s;
//s++;
flag=0;
}
}
return (1);
}
liyunxin 2008-03-20
• 打赏
• 举报

• 打赏
• 举报

show一下自己的成果？...o(∩_∩)o...

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-03-20 12:22

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下