codeforces 710C Magic Odd Square 【杂类】

题目链接 : http://codeforces.com/problemset/problem/710/C
———————————–.
C. Magic Odd Square
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.

Input
The only line contains odd integer n (1 ≤ n ≤ 49).

Output
Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.

Examples
input
1
output
1
input
3
output
2 1 4
3 5 7
6 9 8

—————————————–.
题目大意: 就是给你一个 数N 让你把1~ N^2 填到N*N的 矩阵中 使得每行每列主对角线的和为奇数

解题思路:

附本题代码
—————————.

#include<stdio.h>
int main()
{
    int a[100][100],x;/*初始化*/
    int i,j,m,n,temp;
    printf("输出魔方矩阵n=");
    scanf("%d",&x);
    while(x)
    {
        if(!(x%2))
        {
            printf("你输入了偶数,很遗憾本程序将退出");
            return 0;
        }

        else
        {
            for(i=0; i<x; i++)
                for(j=0; j<x; j++)
                    a[i][j]=0;
            i=0;
            j=x/2;
            a[i][j]=1;
            for(temp=2; temp<=x*x; temp++)
            {
                m=i;
                n=j;
                i--;
                j++;
                if(i<0)      i=x-1;
                if(j>=x)     j=0;
                if(a[i][j]!=0)
                {
                    i=m+1;
                    j=n;
                }

                a[i][j]=temp;
            }
        }
        for(i=0; i<x; i++)
        {
            for(j=0; j<x; j++)
                printf("%4d",a[i][j]);
            printf("\n");
        }
        printf("输出魔方矩阵n=");
        scanf("%d",&x);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值