• [Educational Codeforces Round 16]C. Magic Odd Square


    [Educational Codeforces Round 16]C. Magic Odd Square

    试题描述

    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.

    输入

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

    输出

    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.

    输入示例

    3

    输出示例

    2 1 4
    3 5 7
    6 9 8

    数据规模及约定

    见“输入

    题解

    构造幻方即可。上网搜一下 NOIP2015 Day T1.

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    using namespace std;
    
    const int BufferSize = 1 << 16;
    char buffer[BufferSize], *Head, *Tail;
    inline char Getchar() {
        if(Head == Tail) {
            int l = fread(buffer, 1, BufferSize, stdin);
            Tail = (Head = buffer) + l;
        }
        return *Head++;
    }
    int read() {
        int x = 0, f = 1; char c = getchar();
        while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
        while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
        return x * f;
    }
    
    #define maxn 55
    int n, A[maxn][maxn];
    
    int main() {
    	n = read();
    	
    	int x = 1, y = (n >> 1) + 1, tot = 0;
    	A[x][y] = ++tot;
    	while(tot < n * n) {
    		if(x == 1 && y != n){ x = n; y++; A[x][y] = ++tot; continue; }
    		if(x != 1 && y == n){ x--; y = 1; A[x][y] = ++tot; continue; }
    		if(x == 1 && y == n){ x++; A[x][y] = ++tot; continue; }
    		if(x != 1 && y != n) {
    			if(!A[x-1][y+1]){ x--; y++; A[x][y] = ++tot; continue; }
    			x++; A[x][y] = ++tot;
    		}
    	}
    	
    	for(int i = 1; i <= n; i++) {
    		for(int j = 1; j < n; j++) printf("%d ", A[i][j]);
    		printf("%d
    ", A[i][n]);
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    第五章
    第四章
    第三章
    第二章
    第一章
    configparser-xml-subprocess-shutil
    sys,os,模块-正则表达式
    %----format 格式化字符串---- 生成器---- 迭代器
    python 内置函数
    python 内置函数!
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5806741.html
Copyright © 2020-2023  润新知