C语言 幻方中的和对于大小>37不能正常工作

vwkv1x7d  于 2022-12-11  发布在  其他
关注(0)|答案(1)|浏览(116)

我是一年级的CSD学生,我有一个作业,我需要检查一个正方形是否是一个幻方,并打印“是”或“否”的每一行(包括对角线)和列,满足要求的总和幻方,如果它的每个数字是唯一的,最后,如果它是魔术。例如,它应该像这样:Input of the size of the square + the numbers we put in it-the required output我的问题是我的算法适用于从数字(正方形的大小)1到9的测试。然后测试输入到37+。对于框的大小是37+的测试,每行和每列的总和没有正确相加。
第一个
这是我为上面解释的算法编写的代码,问题是求和。pin[][]是一个有平方输入的数组,pin 1 []是我从pin中生成的数组,用来检查它是否有唯一的数字。非常感谢你的帮助。(我没有包括main和etc,因为这个任务要求两个东西来检查一个平方是否是魔术,并使用参数来创建一个。我只包括了我为检查过程所做的工作。MAXN定义为100

inb24sb2

inb24sb21#

几个问题...
1.一开始我以为sum等溢出了一个int [需要是long long],但事实并非如此

  1. pin数组[可能]太大,无法放入堆栈--需要全局范围。
  2. pin1数组是int pin1[MAXN];,但通过(例如)pin1[i * N + j]访问,因此它需要是pin1[MAXN * MAXN];。并且,还应该是全局范围。
    存在间歇性段故障,因此转到全局范围可以修复这些故障。
    因为我需要在gdb下运行程序,所以我侵入了一个fopen调用,并将scanf更改为fscanf
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 100

#define SCAN(_sym) \
    fscanf(fin,"%d",&_sym)

#ifndef INPUT
#define INPUT   "inp37.txt"
#endif

#if 1
typedef int TYPE;
#else
typedef long long TYPE;
#endif

// NOTE/FIX: these are too large to be function scoped and won't fit on the
// stack
#if 1
int pin[MAXN][MAXN];
int pin1[MAXN * MAXN];
#endif

int
main(int argc, char *argv[])
{
// NOTE/FIX: hack for gdb/debug
#if 1
    FILE *fin = fopen(INPUT,"r");
    if (fin == NULL) {
        perror(INPUT);
        exit(1);
    }
#endif

    if (argc == 2 || argc == 3) {
        if (strcmp("-check", argv[1]) == 0) {
            int p = 0;
            int i;
            int j;
            TYPE sum;
            TYPE sum2;
            int N;
            char ch[] = "YES";
            char unique[] = "YES";
// NOTE/BUG: wrong/insufficient dimension
#if 0
            int pin1[MAXN];
#endif

#if 0
            scanf("%d", &N);
#else
            SCAN(N);
            TYPE N2 = N * N;
            TYPE NGOOD = N;
            NGOOD = (NGOOD * (NGOOD * NGOOD + 1) / 2);
#endif
// NOTE/BUG: too large for stack variable
#if 0
            int pin[MAXN][MAXN];
#endif

            for (i = 0; i < N; i++) {
                for (j = 0; j < N; j++) {
#if 0
                    scanf("%d", &pin[i][j]);
#else
                    SCAN(pin[i][j]);
#endif
                    pin1[i * N + j] = pin[i][j];
                }
            }
            i = 0;
            if (pin1[i] > N2) {
                strcpy(unique, "NO");
            }
            for (i = 0; i < N2; i++) {
                for (j = i + 1; j < N2; j++) {
                    if (pin1[i] == pin1[j] || pin1[i] > N2) {
                        strcpy(unique, "NO");
                    }
                }
            }
            for (i = 0; i < N; i++) {
                sum = 0;
                strcpy(ch, "YES");
                for (j = 0; j < N; j++) {
                    sum = sum + pin[i][j];
                }
                if (sum != NGOOD) {
                    strcpy(ch, "NO");
                    p = 1;
                }
                printf("ROW %d %s\n", i + 1, ch);
            }
            for (j = 0; j < N; j++) {
                sum = 0;
                strcpy(ch, "YES");
                for (i = 0; i < N; i++) {
                    sum = sum + pin[i][j];
                }
                if (sum != NGOOD) {
                    strcpy(ch, "NO");
                    p = 1;
                }
                printf("COLUMN %d %s\n", j + 1, ch);
            }
            sum = 0;
            sum2 = 0;
            strcpy(ch, "YES");
            for (i = 0; i < N; i++) {
                sum = sum + pin[i][i];
                sum2 = sum2 + pin[N - 1 - i][i];
            }
            if (sum != NGOOD) {
                strcpy(ch, "NO");
                p = 1;
            }
            printf("DIAG1 %s\n", ch);
            strcpy(ch, "YES");
            if (sum2 != NGOOD) {
                strcpy(ch, "NO");
                p = 1;
            }
            printf("DIAG2 %s\n", ch);
            printf("UNIQUE %s\n", unique);
            strcpy(ch, "YES");
            if (p == 1) {
                strcpy(ch, "NO");
            }
            printf("MAGIC %s\n", ch);
        }

        else if (strcmp("-create", argv[1]) == 0) {
            int N = atoi(argv[2]);
#if 0
            int pin[MAXN][MAXN];
#endif
            int row = N - 1;
            int col = N / 2;

            for (int i = 1; i <= N * N; i++) {
                int tempr = row;
                int tempc = col;

                pin[row][col] = i;
                row++;
                col++;
                if (row == N) {
                    row = 0;
                }
                if (col == N) {
                    col = 0;
                }
                if ((pin[row][col] >= 1) & (pin[row][col] <= N * N)) {
                    row = tempr - 1;
                    col = tempc;
                }
            }
            printf("%d\n", N);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    printf("%d ", pin[i][j]);
                }
                printf("\n");
            }
        }
        else {
            printf("Such action cannot be done");
        }
    }
    else if (argc > 3) {
        printf("Too many arguments supplied.\n");
    }
    else {
        printf("At least one argument expected.\n");
    }

#if 1
    fclose(fin);
#endif

    return 0;
}

在上面的代码中,我使用了cpp条件语句来表示旧代码和新代码:

#if 0
// old code
#else
// new code
#endif

#if 1
// new code
#endif

注意:通过unifdef -k运行文件可以清除此问题
下面是您发布的“big”输入的程序输出:

ROW 1 YES
ROW 2 YES
ROW 3 YES
ROW 4 YES
ROW 5 YES
ROW 6 YES
ROW 7 YES
ROW 8 YES
ROW 9 YES
ROW 10 YES
ROW 11 YES
ROW 12 YES
ROW 13 YES
ROW 14 YES
ROW 15 YES
ROW 16 YES
ROW 17 YES
ROW 18 YES
ROW 19 YES
ROW 20 YES
ROW 21 YES
ROW 22 YES
ROW 23 YES
ROW 24 YES
ROW 25 YES
ROW 26 YES
ROW 27 YES
ROW 28 YES
ROW 29 YES
ROW 30 YES
ROW 31 YES
ROW 32 YES
ROW 33 YES
ROW 34 YES
ROW 35 YES
ROW 36 YES
ROW 37 YES
COLUMN 1 YES
COLUMN 2 YES
COLUMN 3 YES
COLUMN 4 YES
COLUMN 5 YES
COLUMN 6 YES
COLUMN 7 YES
COLUMN 8 YES
COLUMN 9 YES
COLUMN 10 YES
COLUMN 11 YES
COLUMN 12 YES
COLUMN 13 YES
COLUMN 14 YES
COLUMN 15 YES
COLUMN 16 YES
COLUMN 17 YES
COLUMN 18 YES
COLUMN 19 YES
COLUMN 20 YES
COLUMN 21 YES
COLUMN 22 YES
COLUMN 23 YES
COLUMN 24 YES
COLUMN 25 YES
COLUMN 26 YES
COLUMN 27 YES
COLUMN 28 YES
COLUMN 29 YES
COLUMN 30 YES
COLUMN 31 YES
COLUMN 32 YES
COLUMN 33 YES
COLUMN 34 YES
COLUMN 35 YES
COLUMN 36 YES
COLUMN 37 YES
DIAG1 YES
DIAG2 YES
UNIQUE YES
MAGIC YES

相关问题