注册 登录
编程论坛 C++教室

求N*N的数字旋转方阵(N为偶数没问题,N为质数N*N的那个数就为乱码)

zfw1994 发布于 2014-03-31 21:54, 476 次点击
#include <iostream>
#include<iomanip>
using namespace std;
void main ()
{
    int N,n,s=1,a[10][10];
    cout<<"Plese input number:";
    cin>>N;
    if(N%2==0)
        n=N/2;
    else
        n=(N-1)/2;
    for (int d=0;d<=n-1;d++)
    {
        for(int j=d;j<N-1-d;j++)
            a[j][d]=s++;
        for(int k=d;k<N-d-1;k++)
            a[N-1-d][k]=s++;
        for(int l=N-d-1;l>d;l--)
            a[l][N-1-d]=s++;
        for(int m=N-1-d;m>=1+d;m--)
            a[d][m]=s++;

    }
    cout<<endl<<endl;
    for(int c=0;c<=N-1;c++)
    {
        for(int b=0;b<=N-1;b++)
            cout<<setw(6)<<a[c][b];
        cout<<endl;
    }
}
0 回复
1