zoukankan      html  css  js  c++  java
  • P2615 神奇的幻方

    题目链接:https://www.luogu.com.cn/problem/P2615

    题目解析:

      说实话,刚看到时被吓到了,感觉无从下手。然后才发现这是个大水题。直接模拟即可

      先定义好n * n的数组,然后依次填入数字,根据上一个填入数字的下标选择当前这个数要填入的下标是多少。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int arr[45][45];
     4 int main() {
     5     int n;
     6     cin >> n;
     7     int t = 1; //t是每次要填的数
     8     int x = 1, y = n / 2 + 1; //第一个点要在第一行的中间
     9     arr[x][y] = t;
    10     int sum = n * n;
    11     while (t <= sum) {
    12         t++; //每次填了一个数后,t++
    13         if (x == 1 && y != n) {
    14             x = n;
    15             y = y + 1;
    16         } else if (y == n && x != 1) {
    17             y = 1;
    18             x = x - 1;
    19         } else if (x == 1 && y == n) {
    20             x = x + 1;
    21         } else if (x != 1 && y != n) {
    22             if (arr[x - 1][y + 1] == 0) {
    23                 x = x - 1;
    24                 y = y + 1;
    25             } else {
    26                 x = x + 1;
    27             }
    28         }
    29         arr[x][y] = t;
    30     }
    31     for (int i = 1; i <= n; i++) {
    32         for (int j = 1; j <= n; j++) {
    33             cout << arr[i][j] << " ";
    34         } 
    35         cout << endl;
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/fx1998/p/13720552.html
Copyright © 2011-2022 走看看