/ 科创班 /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 4ms 340.0 KiB
#2 Accepted 3ms 368.0 KiB

代码

#include <iostream>

using namespace std;

int main()
{
    int a[15][15], p = 1, n, i, j, k;
    while(p == 1)
    {
        cin >> n;
        if((n != 0) && (n <= 15) &&(n % 2 != 0))
        {
            p = 0;
        }
    }
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            a[i][j] = 0;
        }
    }
    j = n / 2 + 1;
    a[1][j] = 1;
    for(k = 2; k <= n * n; k++)
    {
        i = i - 1;
        j = j + 1;
        if((i < 1) && (j > n))
        {
            i = i + 2;
            j = j - 1;
        }
        else
        {
            if(i < 1)
            {
                i = n;
            }
            if(j > n)
            {
                j = 1;
            }
        }
        if(a[i][j] == 0)
        {
            a[i][j] = k;
        }
        else
        {
            i = i + 2;
            j = j - 1;
            a[i][j] = k;
        }
    }
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

信息

递交者
类型
递交
题目
输出n阶魔方阵
题目数据
下载
语言
C++
递交时间
2018-07-15 19:22:45
评测时间
2018-07-15 19:22:45
评测机
分数
20
总耗时
7ms
峰值内存
368.0 KiB