c-doing螺旋矩阵实现中的分段错误运行时错误

c-doing螺旋矩阵实现中的分段错误运行时错误,c,algorithm,matrix,runtime-error,spiral,C,Algorithm,Matrix,Runtime Error,Spiral,我在HackerAth上读到一个问题,它要求我们打印一个螺旋矩阵NN的两条对角线的和,输入N。[0][0]=NN的螺旋矩阵 以1为中心,即 16 15 14 13 5 4 3 12 6 1 2 11 7 8 9 10 我已经为此实现了代码,但给出了分段或运行时错误 当输入N>=10^5时 我不知道发生了什么事 #include <stdio.h> int main(void) { long long int n,test; scan

我在HackerAth上读到一个问题,它要求我们打印一个螺旋矩阵NN的两条对角线的和,输入N。[0][0]=NN的螺旋矩阵 以1为中心,即

16  15  14  13

5   4   3   12

6   1   2   11

7   8   9   10
我已经为此实现了代码,但给出了分段或运行时错误 当输入N>=10^5时

我不知道发生了什么事

#include <stdio.h>

int main(void) {
long long int n,test;
scanf("%lld",&test);
while(test--){
    scanf("%lld",&n);
    long long int p=n*n;
    long long int r1=0,r2=n-1,c1=0,c2=n-1,i,j;
    long long int a[n][n];
    while(p>=1)
    {
        for(i=c1;i<=c2;i++)
        {
            a[r1][i]=p--;
        }
        for(j=r1+1;j<=r2;j++)
        {
            a[j][c2]=p--;
        }
        for(i=c2-1;i>=c1;i--)
        {
            a[r2][i]=p--;
        }
        for(j=r2-1;j>=r1+1;j--)
        {
            a[j][c1]=p--;
        }
        c1++;
        c2--;
        r1++;
        r2--;
    }
    long long int sum=0;
       for ( i = 0, j =0; i< n && j < n; i++, j++) {
              sum = sum + a[i][j];

       }

       for ( i=0,j=n-1 ; i<n && j>=0 ; i++, j--) {
              sum= sum + a[i][j];

       }

           printf("%lld\n",sum%1000000009);


}
return 0;
}
#包括
内部主(空){
长整型n,测试;
扫描频率(“%lld”、&test);
而(测试--){
scanf(“%lld”、&n);
长整型p=n*n;
长整型r1=0,r2=n-1,c1=0,c2=n-1,i,j;
长整型[n][n];
而(p>=1)
{
对于(i=c1;i=r1+1;j--)
{
a[j][c1]=p--;
}
c1++;
c2--;
r1++;
r2--;
}
长整型和=0;
对于(i=0,j=0;i
长整型[n][n]=10^5,则代码>太大,无法在堆栈中进行保护

替换为
long(*a)[n]=malloc(sizeof(long-long[n][n])


最后一个元素是n*n:(4*4=16)
前角元素是最后一个元素-(n-1):16-(4-1)=16-3=13
预转角元件:13-3=10
预转角元件:10-3=7
(4次)


一种不使用2D数组的方法

#include <stdio.h>

int main(void){
    long long n;//The length of the side
    long long lastValue;//n*n
    long long sum = 0;

    scanf("%lld", &n);

    sum += (n & 1);//center (1) if odd, Not counting duplicates;

    while(n >= 2){
        lastValue = n * n;
        for(int i = 0; i < 4; ++i){//4 times
            sum += lastValue;
            lastValue -= n - 1;//pre lastValue..
        }
        n -= 2;
    }
    printf("Ans.%lld\n", sum);
    return 0;
}
#包括
内部主(空){
long n;//边的长度
long lastValue;//n*n
长和=0;
scanf(“%lld”、&n);
sum+=(n&1);//中心(1)如果为奇数,则不计算重复项;
而(n>=2){
lastValue=n*n;
对于(int i=0;i<4;++i){//4次
总和+=最后一个值;
lastValue-=n-1;//pre-lastValue。。
}
n-=2;
}
printf(“Ans.%lld\n”,总和);
返回0;
}

寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建一个最小、完整且可验证的示例。欢迎使用堆栈溢出!听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:。使用
malloc
而不是VLA。如何@BLUEPIXY显示
long-long(*a)[n]=malloc(sizeof(long-long[n][n])代替。但它不会给出10000000的预期o/p,因为正确的o/p为679604006,总和为%(10^9+9)<代码>lastValue-=n-1-->
lastValue-=n-1;总和%=1000000009
#include <stdio.h>

int main(void){
    long long n;//The length of the side
    long long lastValue;//n*n
    long long sum = 0;

    scanf("%lld", &n);

    sum += (n & 1);//center (1) if odd, Not counting duplicates;

    while(n >= 2){
        lastValue = n * n;
        for(int i = 0; i < 4; ++i){//4 times
            sum += lastValue;
            lastValue -= n - 1;//pre lastValue..
        }
        n -= 2;
    }
    printf("Ans.%lld\n", sum);
    return 0;
}