#include<stdio.h>
int main()
{
int size,i;
printf("Enter size of square: ");
scanf("%d",&size);
int square[size][size];
int cord_x = 1;
int cord_y = 0;
for(i=1;i<=(size*size);i++)
{
if(cord_x>(size-1))
{
cord_x -= size+1;
}
if(cord_y>(size-1))
{
cord_y -= size+1;
}
if(square[cord_y][cord_x] == NULL)
{
square[cord_y][cord_x] = i;
cord_y += 1;
cord_x += 1;
}
else
{
continue;
}
show(square,size);
}
return 0;
}
show(array,s)
int s,array[s][s];
{
int i,j;
for(i=0;i<s;i++)
{
for(j=0;j<s;j++)
{
printf("%d ",array[j][i]);
}
}
printf("n");
}
尝试编写一个魔方(n × n矩阵中每一行的数字之和等于)的程序。我是C的新手,不知道为什么它不能工作。
我使用了以下算法:
- 将初始数字放在顶部行中间(例如k)
- 向上移动一行,向右移动一列,放置k+1
- 如果移动到顶部行上方,请移到底部行(列也一样)
- 如果移动将带你到预填充的方块或你移出右上角的方块,将k+1放在k的正下方
编辑:把正方形[size][size]放在scanf后面
预期输出:
8 1 6
3 5 7
4 9 2
编译器输出:
0 0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 2 0
if(cord_x>(size-1))
{
cord_x -= size+1;
}
if(cord_y>(size-1))
{
cord_y -= size+1;
}
你看,当cord_x == size
时,新值将是-1
。
最好将数组索引包装在自增点,因此替换
cord_y += 1;
cord_x += 1;
cord_y = (cord_y + 1) % size;
cord_x = (cord_x + 1) % size;
并移除故障范围测试。
我使用了以下算法:
- 将初始数字放在顶部行中间(例如k)False:你从第二列和第一行开始(总是,独立于变量
size
-这应该是奇数,但你没有这样说)这是很重要的,使两个对角线加起来等于相同的行/列 值 - 上移一行,右移一列,放置k+1False:向下移动一行,向右移动一列。您需要减少行(向上移动)并增加列(向右移动)。
- 如果移动到上行或左列以上,请转到下行(或左列)。你试着用一种非常复杂的方式来实现它。当你到达底部(
col == size
,递增后),你必须移动到顶部(col = 0
,或col -= size
,因为你已经增加了它,你需要移动size
行,而不是size + 1
) - 如果移动带你到预填充的正方形或你移动到右边的正方形,把k+1放在k下面,这是一行增量。
这是一个已知的魔方公式(一个用数字1
到size*size
填充方阵的公式,所有行加到相同的数字,所有列加到相同的数字,两个大对角线也是如此。)
事情(在解决了所有你做得不好的事情之后)应该以类似于这样的方式进行:
/* ms.c -- magic square.
* Author: Luis Colorado.
* Date: Fri 03 Sep 2021 02:08:34 PM EEST
* Copyright: (C) 2021 Luis Colorado. All rights reserved.
* License: BSD.
*/
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 1023
#define DFLT 11
int square[MAX][MAX];
int sum_main_diag,
sum_anti_diag,
sum_cols[MAX],
sum_rows[MAX];
char DOUBLE_LINE[] = "========================";
char SINGLE_LINE[] = "------------------------";
int main(int argc, char **argv)
{
int opt; /* option */
int size = DFLT;
while ((opt = getopt(argc, argv, "n:")) >= 0) {
switch (opt) {
case 'n': size = atoi(optarg);
if (size < 1 /* size must be >= 1 */
|| size > MAX /* and <= MAX */
/* || !(size & 1) */) /* ...and odd, commented (see below) */
{
fprintf(stderr,
"N defaulting to %d, as provided "
"value (%s) was invalidn",
DFLT, optarg);
size = DFLT;
}
break;
} /* switch */
}
int row = 0, /* top row */
col = size/2, /* middle cell */
total = size * size, /* total number of cells */
i; /* number to put in the cell */
for (i = 1; i <= total; i++) {
square[row][col] = i; /* fill the square */
if (row == col) { /* add to anti_diag */
sum_anti_diag += i;
}
if (row + col + 1 == size) { /* add to main diag */
sum_main_diag += i;
}
sum_cols[col] += i; /* add to col */
sum_rows[row] += i; /* add to row */
int nxtrow = row, /* next row to go */
nxtcol = col; /* next col to go */
nxtrow--;
if (nxtrow < 0) { /* top row */
nxtrow = size - 1; /* rules 2 & 3 */
}
nxtcol++;
if (nxtcol >= size) { /* right column */
nxtcol = 0; /* rules 2 & 3 */
}
if (square[nxtrow][nxtcol]) { /* rule four */
/* next cell is the located below the initial */
nxtrow = row + 1;
if (nxtrow >= size) { /* check boundary, not needed */
nxtrow = 0;
}
nxtcol = col;
}
col = nxtcol; /* ... update */
row = nxtrow;
}
/* print them all */
int digs = snprintf(NULL, 0, "%d", total);
int digs_sums = snprintf(NULL, 0, "%d", total*(total+1)/2/size);
for (row = 0; row < size; row++) {
printf("%*s +", digs_sums, "");
for (col = 0; col < size; col++)
printf("%.*s+", digs_sums, SINGLE_LINE);
printf("n%*d |", digs_sums, row);
for (col = 0; col < size; col++) {
printf("%*d|", digs_sums, square[row][col]);
int val = square[row][col];
}
printf(" ==> %*dn", digs, sum_rows[row]);
}
printf("%*s +", digs_sums, "");
for (col = 0; col < size; col++)
printf("%.*s+", digs_sums, DOUBLE_LINE);
printf("n%*d/|", digs, sum_anti_diag);
for (col = 0; col < size; col++)
printf("%*d|", digs, sum_cols[col]);
printf("\ %dn", sum_main_diag);
return 0; /* OK exit code */
}
将输出如下内容:
$ ms -n 3
+--+--+--+
0 | 8| 1| 6| ==> 15
+--+--+--+
1 | 3| 5| 7| ==> 15
+--+--+--+
2 | 4| 9| 2| ==> 15
+==+==+==+
15/|15|15|15| 15
$ ms -n 5
+--+--+--+--+--+
0 |17|24| 1| 8|15| ==> 65
+--+--+--+--+--+
1 |23| 5| 7|14|16| ==> 65
+--+--+--+--+--+
2 | 4| 6|13|20|22| ==> 65
+--+--+--+--+--+
3 |10|12|19|21| 3| ==> 65
+--+--+--+--+--+
4 |11|18|25| 2| 9| ==> 65
+==+==+==+==+==+
65/|65|65|65|65|65| 65
$ ms -n 6 # I've commented the odd test to see that the magic square is invalid.
+---+---+---+---+---+---+
0 | 19| 27| 35| 1| 9| 17| ==> 108
+---+---+---+---+---+---+
1 | 26| 34| 6| 8| 16| 24| ==> 114
+---+---+---+---+---+---+
2 | 33| 5| 7| 15| 23| 25| ==> 108
+---+---+---+---+---+---+
3 | 4| 12| 14| 22| 30| 32| ==> 114
+---+---+---+---+---+---+
4 | 11| 13| 21| 29| 31| 3| ==> 108
+---+---+---+---+---+---+
5 | 18| 20| 28| 36| 2| 10| ==> 114
+===+===+===+===+===+===+
123/|111|111|111|111|111|111| 93
$ ms
+---+---+---+---+---+---+---+---+---+---+---+
0 | 68| 81| 94|107|120| 1| 14| 27| 40| 53| 66| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
1 | 80| 93|106|119| 11| 13| 26| 39| 52| 65| 67| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
2 | 92|105|118| 10| 12| 25| 38| 51| 64| 77| 79| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
3 |104|117| 9| 22| 24| 37| 50| 63| 76| 78| 91| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
4 |116| 8| 21| 23| 36| 49| 62| 75| 88| 90|103| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
5 | 7| 20| 33| 35| 48| 61| 74| 87| 89|102|115| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
6 | 19| 32| 34| 47| 60| 73| 86| 99|101|114| 6| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
7 | 31| 44| 46| 59| 72| 85| 98|100|113| 5| 18| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
8 | 43| 45| 58| 71| 84| 97|110|112| 4| 17| 30| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
9 | 55| 57| 70| 83| 96|109|111| 3| 16| 29| 42| ==> 671
+---+---+---+---+---+---+---+---+---+---+---+
10 | 56| 69| 82| 95|108|121| 2| 15| 28| 41| 54| ==> 671
+===+===+===+===+===+===+===+===+===+===+===+
671/|671|671|671|671|671|671|671|671|671|671|671| 671
$ _
(参数-n
允许1到1023之间的任何奇数,默认值-例如未使用选项-为11)