2 like 0 dislike
2.5k views

古書上有記載:『九宮之義,法之靈龜。二四為肩,六八為足,左七右三,載九履一,五居中央。』

其實他是在說一個3X3的魔方陣,就像下面這個一樣。

魔方陣神奇的地方就在於不管是直的、橫的還是對角線,他的和都是一樣。用一個固定的規則把數字填入奇數大小的方陣中,就會產一個魔方陣,其規則如下。

 

照這樣的規則把數字填入5X5的方陣中就會產生以下的魔方陣。

請寫一個程式,輸入一個奇數代表方陣的大小,請依照給予的規則把數字填進去。

 

輸入說明:

會輸入一個正奇數,代表方陣的大小。

 

輸出說明:

請輸出整個魔方陣

 

輸入範例:

5

 

輸出範例:

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

 

[Normal] Coding (C) - asked in 2017-1 程式設計(一)AD by (30k points)
ID: 38150 - Available when: Unlimited - Due to: Unlimited
| 2.5k views

6 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>

int main()
{
* * * * ** ** *** * * * m[100][100] = {0};
    int n, k, x, y;
** * ***** * * *** ** ** ** * ** **** &n);
    k = 1;
* *** * * * * * * * ** ** *** <= n * n)
    {
* * * *** * ** **** * ** * ** ** **** * ***** == 1)
** * **** * **** * * ** * **** *** *** ** ** * ** *
* ** *** * ** * ** * * * *** *** * * * * * ** *** * **** *** = n / 2;
* ***** ** * * ** ** * ** ** * * ** ** ** * * ** * * ** * * = 0;
**** ** ** * ******* * ** ** ** * * * *
***** *** ***** ** * * * * ** * * * * * * *
** * ** * * *** * * ***** * **
* ***** *** * *** * * *** * * *** * **** ** * **** ** ** * *** * *** * - 1 + n) % n][(x + 1) % n] == 0)
* **** * * *** ***** * ** * ** **** * * *** * * * **** *** ** **
* *** *** *** **** * *** * ** * ** * * **** * ** ******** *** ** * * * *** * ** * = (x + 1) % n;
** ** *** *** * ** * * ****** ** ** *** * * **** * * * * ***** * ** * **** * * = (y - 1 + n) % n;
*** ** * * * *** * * * ** * * * ** * * * * ** **** * **** * * * ***
***** * * ** ** ****** ** * ** ** * * * * ** * * * *
* ******** * ** ** ** ****** * ** ** ** * * ***** * * * * ** ** * ******
****** **** * ** ** * * * *** **** * *** * ****** * * ** ** ** ** * * ** *** *** = (y + 1) % n;
* ****** * * * *** * * ** * ** **** *** * * * * * * *
* ** ****** * * ****** * ** * * ** ** * *
********* ** * * *** * * * ** * * **** * = k;
** * * * ** * *** * **** ** * * * * *** * ** **** ++;
    }
    for(y = 0;y < n;y ++)
    {
* **** *** * ** **** ***** * * * ** * * * * ** ** = 0;x < n;x ++)
* * ***** ** ** * * * ** * * ******** *** * *** ** ** **** ***** * *** ",m[y][x]);
* * ******* * **** * * * * **** *** ** ** * * * * ** * *
    }
}
answered
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
include<stdio.h>

int main()
{
* ** * * ** ** * * ** * * m[100][100] = {0};
    int n, k, x, y;
*** ** ***** * ** * * * * * &n);
    k = 1;
* ** **** * * * * * <= n * n)
    {
* ** * ** ** * * ** * ** ** **** ** * == 1)
*** * ** ** * * * ** * * * * * **
*** *** *** **** * * *** ******** * * ** ** ** ** ** = n / 2;
* **** * ** * ***** * ** *** ***** * ** ** ** **** ** ** * **** = 0;
* **** ** ** *** * *** * *** * *** *
** *** * **** * * ** **** * ** * * ** *
**** *** *** * * * * ** *** *** * ** * ** **
* * * * * *** ***** * * *** * * ** ** *** * * * * ** *** * - 1 + n) % n][(x + 1) % n] == 0)
* * ******* ** ** *** * * * ** **** * ** *** ** ** ** **** *
* ***** * * ***** * * * * * * *** * * * * * * ** * * ** *** ****** *** ** ***** *** * = (x + 1) % n;
** **** *** * * * ** * ***** * ***** * ** * ** * * *** * ** * * ** *** **** *** ** * = (y - 1 + n) % n;
* **** *** ** ** ** **** ****** *** *** * *** * * ****** ** ** *
** * * ** * ** * *** ****** ** **** *** *** **** ** * * ** * * ***
** * *** *** ** ** * * ***** ***** ***** **** * ** * ** * *** *
** * * ** * ** * * * ***** * * ** * ** *** * * ** * * *** * * * * * * * * * * *** = (y + 1) % n;
**** **** *** **** * * **** * ****** ******** ** * * * *****
* ** * * ** * * * ** *** ******* * * **
** *** *** * * * ****** *** * ***** ******* = k;
**** ** * * * * ** *** ** * * ** ** **** * * * ++;
    }
    for(y = 0;y < n;y ++)
    {
* ** * * **** * * * ** * * * * * = 0;x < n;x ++)
* *** *** * ** * * *** ** * * * ** * * * ** * * * * *** * ",m[y][x]);
** ** * * *** *** ***** * ** * * ** * * * **** * * ****
    }
}
answered by (190 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
prog.c:1:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
 include<stdio.h>
        ^
0 like 0 dislike
Hidden content!
** *** * * * ***

int main(){
* ** *   int * *
* ** ** * * *** * * **** * * *****
** * ** int ** *** * * *
* ** * * * *** while(k <= n * n){
** * * ** ** ** * *** if(k==1){
** ** ***** *** *   * ** *** x=0;
** * *** ** * ** * * *** ** ******* y=n/2;
* *** ** *** * ** * * * }
**** * * * * * * ** else{
** ** **** * **** * * * ** * * * * * * * * ** *** *
* ** * *             ** *
***         * * *** * * * * *
* * ** ** ** *** *** ** **** }
* *** ** ** * * * ** * * * ** ** ** * ** else{
*** *** * ** * * * * *** * * ** * * **** * ** * * * * * **
* * ** * ** ** * ** ** * *** ** * }
** * ** * * * * * ** }
**** * *** * * *** * * *
*** * *** * **** ** * ***** * k++;
* *** * *** ****** * * ** * * * ** *** * ** ***
* *** **** }
*** * * *** *** ** * **** **
* * * * * * * * * * * ** * * ** * *
* * ** * * *** * ** *** ** *** ** *** **** * **
* **** * * * *** * * * }
* * * ****** *** * ****** * * * ** *
* ** ** * * }
}
answered by (192 points)
edited by
0 0
prog.c: In function 'main':
prog.c:6:2: error: variable-sized object may not be initialized
  int arr[n][n]={0};
  ^~~
prog.c:6:17: warning: excess elements in array initializer
  int arr[n][n]={0};
                 ^
prog.c:6:17: note: (near initialization for 'arr[0]')
prog.c:6:2: warning: excess elements in array initializer
  int arr[n][n]={0};
  ^~~
prog.c:6:2: note: (near initialization for 'arr')
0 0
prog.c: In function 'main':
prog.c:6:2: error: variable-sized object may not be initialized
  int arr[n][n]={0};
  ^~~
prog.c:6:17: warning: excess elements in array initializer
  int arr[n][n]={0};
                 ^
prog.c:6:17: note: (near initialization for 'arr[0]')
prog.c:6:2: warning: excess elements in array initializer
  int arr[n][n]={0};
  ^~~
prog.c:6:2: note: (near initialization for 'arr')
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>

int main()
{
** * * **** * * **** * ******* m[100][100] = {0};
    int n, k, x, y;
* ** ** * *** *** ** * ** * *** * * &n);
    k = 1;
* * ** * * * * * * * * ** <= n * n)
    {
** * * * **** * * ***** ** * * ** * * == 1)
** *** ** ** * ** * **** ** ** *** **
** * * **** * * * *** * * * * * ** * ****** * *** *** * ***** * ** = n / 2;
** ** ***** *** * ** ****** * ** * * *** *** * * ** ** * = 0;
* * ******* * * * * * ** * * ** * **
** **** *** ** * ** *** * *** * * * * ** *** * *
*** * **** * * * * ** * ** ** * * ** **
** **** * *** *** ** *** ********** * *** ** * * ** ** * * * * ** * * - 1 + n) % n][(x + 1) % n] == 0)
*** **** * **** * * * ** *** ****** ****** * ** * ** ** *** * * * **
* * * ** * ** ** * * * **** * ***** * **** * *** * ** * ******* ** **** * * * = (x + 1) % n;
* ** ** * ** * * *** * * * ** * * ****** *** * * * * **** * * * **** **** * ** ** * * = (y - 1 + n) % n;
*** *** ********* * ****** *** ** ** *** ** * *** * ** ********* ** * ** * *
** * *********** * * * **** * * * **** * *** * * * * ** *** *
* ******* * * * *** ** * **** * * ** * * **** * *** ** ** ** **
* * ** *** ** ** * ** *** ** ** * *** * ** * ****** ** ** * * * *** * * ***** **** *** = (y + 1) % n;
** * ** * **** *** ** * * *** ** *** * ** ** ** * ** *** ** ** *
* * ** * * ***** * ** * ** ***** **** *****
* * ** ** * * * *** ** * ******* * * ** * = k;
******** * * * ** ** * * * *** * ** ++;
    }
    for(y = 0;y < n;y ++)
    {
* * ** * * *** * ** ** ***** ** ** = 0;x < n;x ++)
* * * * * **** *** * * **** ***** * * * * ******* *** ",m[y][x]);
* * ** * **** **** * * * *** * * * * * * ******* * ** * * * * *
    }
}
answered by (174 points)
0 0
Case 0: Correct output
Case 1: Correct output
0 like 0 dislike
Hidden content!
** * *** * * * *

int * * *
{
* **   int * *** ***
* ** ** * * * * * * ** ** * * * * * *
**** * k=1;
** * ***** * while(k <= n*n)
* ** * **** {

* ** * ** * ** **** ** ** if(k==1)
* ** * * ** *** ** * {
* * * * ** ** *** * **** * *** ** * x=n/2;
** *** ** ** * * * * ** * * ** * * y=0;
**** ** * * * * * *** * * }
* * * * **** ** ** * else
** * ** * *** * *** * {
*** * *** * *** **       if(m[(y -1 +n)%n][(x +1)%n]==0)
*** **     *** * ***** * ** {
** * * * ** * ** ** * * * * * * *** * ** x = (x+1)%n;
** * **** * *** * *** * * *** * * **** ** *** ** ***
* **** * * * ** * ** * * * * }
*** ******* * ** * * ** ** *** ** else
* ** * ** ***** ******* **** * {
**** ** ** * ******* * *** * * ** ** * * * y = (y+1)%n;
** * ** *** ****** * * * ** *** ** }
* ** * ** ***** * * * * }
* * * * ** * ** m[y][x] = k;
* * ** * ** * ** * k++;
*** * * * }
* * * *** * **** * ** * *
* * * * {
* *** * **** * **** *** * ** *
** * * * **** * * * * *** * * * * * *** * * * *****
* ** * * * ** * ** ** * * * * **
** ** ** ** * }
*** ** *** * return 0;
}
answered by (160 points)
0 1
Case 0: Wrong output
Case 1: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>

int main()
{
* ** * **** * * *** * ** ***** m[100][100] = {0};
    int n, k, x, y;
** *** * *** * * * * *** *** &n);
    k = 1;
**** ******* *** * <= n * n)
    {
** * ** ** * * *** * * * * * * * ** * * * * == 1)
*** ** *** * * *** ** * *** * * * *
** * * * * ** *** *** * ****** ** * * *** * **** *** * ** = n / 2;
* * * * * *** * ******** **** * *** * ** ** ** * ** * * *** * * = 0;
** * * * ** * *** * * ** * * ** ** * ****
* * * ** * * ** *** * *** * *** **** *
* ** *** ******* * * * ******** ** **** * *
** ** *** ** * ** * * * * * ** * **** * ** ** *** ** ** *** ** * * * - 1 + n) % n][(x + 1) % n] == 0)
* * * ** * * ** * * * * * * * * * * * * * *** * * * * *****
* * * * *** ** ** *********** * ** * ** * * ***** * * * ** ** * * ** * * * *** ** = (x + 1) % n;
***** * ** * * * **** *** * * * * * ** * ** * * *** * * ** * * *** ** *** ******* * = (y - 1 + n) % n;
* ***** * * ** ** *** ** *** * * * * * ** ****** **** ******* * ***
* * **** ** * * ***** ** * ** * ** * * ****** ** ** ** ** * * *
* * * * ** ** * ***** ** ****** * * ** * * * * * ** * ** * * * ** **
** * * ** ** *** **** * ** ********** *** **** * * * ** ****** ** ** ** * * * ** **** * = (y + 1) % n;
* * * * * * * ** *** ****** * ** * * ** * * * * * * *** * ** **
** * *** * * * ** *** ***** * * *** *** * * **
* ** * * * *** * * * * *** * **** ** *** * * = k;
* * * * * *** **** ** ** * ** * * * * * * ** * * ++;
    }
    for(y = 0;y < n;y ++)
    {
* * ********* ** * ***** * *** * ** * = 0;x < n;x ++)
** * ** ** **** **** * * ** ***** * ** * * * ** ** ***** * * ",m[y][x]);
*** * ** * **** **** * * ***** ** **** * ** ** * ****
    }
}
answered by (30k points)
0 0
Case 0: Correct output
Case 1: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.17.23
©2016-2025

Related questions

4 like 0 dislike
5 answers
[Exam] asked Dec 8, 2017 in 2017-1 程式設計(一)AD by Shun-Po (18k points)
ID: 36174 - Available when: 2017-12-08 18:30 - Due to: 2017-12-08 21:00
| 2.2k views
2 like 0 dislike
3 answers
[Normal] Coding (C) - asked Dec 20, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38168 - Available when: Unlimited - Due to: Unlimited
| 1.1k views
2 like 0 dislike
5 answers
[Normal] Coding (C) - asked Dec 20, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38165 - Available when: Unlimited - Due to: Unlimited
| 1.8k views
2 like 0 dislike
6 answers
[Normal] Coding (C) - asked Dec 20, 2017 in 2017-1 程式設計(一)AD by 楊修俊 (30k points)
ID: 38162 - Available when: Unlimited - Due to: Unlimited
| 1.8k views
12,783 questions
183,442 answers
172,219 comments
4,824 users