RootKings 2018-11-11 13:11 采纳率: 100%
浏览 435
已采纳

这个题目搞不清楚,请求大神解答,附自己写的(不知道自己哪里错了)

求蛇形输入方阵
例如:N=4
7 13 14 16
6 8 12 15
2 5 9 11
1 3 4 10

 #include<stdio.h>
int main()
{
    int i,j,k,count,temp,N,n;
    printf("please input N");
    scanf("%d",&N);
    n=N-1;count=1;k=1;j=0;i=n;
    int a[N][N];
    while(count<=N)
    {
        for(temp=0;temp<count;temp++)
        {
            if(count%2!=0)
            {
                a[i--][j--]=k++;
                if(j<0)
                {
                    j=0;
                    continue;

                }
            }
            if(count%2==0)
            {
                a[i++][j++]=k++;
                if(i>n)
                {
                    i=n;
                    continue;
                }

            }
        }
        count++;
    }
    j=n-1;j=n;count=1;
    while(count<N)
    {
        for(temp=0;temp<N-count;temp++)
        {
            if(count%2!=0)
            {
                a[i--][j--]=k++;
                if(i<0)
                {
                    i=0;j+=2;
                    continue;
                }
            }
            if(count%2==0)
            {
                a[i++][j++]=k++;
                if(i>n)
                {
                    j=n;i-=2;
                    continue;
                }
            }
        }
        count++;
    }
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        printf("%d   ",a[i][j]);
        printf("\n");
    }
    return 0;
}
  • 写回答

1条回答 默认 最新

  • Italink 2018-11-11 15:44
    关注

    主要是弄完一个角之后,再另一个角的起点判断有问题

     #include<stdio.h>
    int main()
    {
        int i, j, k, count, temp, N, n;
        printf("please input N");
        scanf("%d", &N);
        n = N - 1; count = 1; k = 1; j = 0; i = n;
        int a[10][10];
        while (count <= N)
        {
            for (temp = 0; temp < count; temp++)
            {
                if (count % 2 != 0)
                {
                    a[i--][j--] = k++;
                    if (i < 0)
                        i = 0;
                    if (j < 0)
                        j = 0;
                }
                else if (count % 2 == 0)
                {
                    a[i++][j++] = k++;
                    if (i > n)
                        i = n;
                    if (j > n) 
                        j = n;
    
                }
            }
            count++;
        }
        if (N % 2 != 0)     //输入完一个角之后,起点跟N的奇偶有关
            j++;
        else
            i--;
        count = 1;
        while (count < N)
        {
            for (temp = 0; temp < N - count; temp++)
            {
                if ((N-count) % 2 != 0)
                {
                    a[i--][j--] = k++;
                    if (i < 0) {
                        i = 0;
                        j += 2;
                    }
                }
                if ((N-count) % 2 == 0)
                {
                    a[i++][j++] = k++;
                    if (j > n) {
                        j = n;
                        i -= 2;
                    }
    
                }
            }
            count++;
        }
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++)
                printf("%3d   ", a[i][j]);  //设置宽度为3
            printf("\n");
        }   return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。