lwjppz 2018-11-20 02:02 采纳率: 54.5%
浏览 380
已采纳

求助,这题要这么做啊?

图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-11-20 04:58
    关注
    // Q713919.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include "stdio.h"
    #include "stdlib.h"
    
    int movenext(int *x, int *y, int *dir, int n)
    {
        if (*x == *y && *y == n - 1) return false;
        if (*x == 0 && *y == 0)
        {
            *y = 1; *dir = 1;
        }
        else if (*y == 0 && *dir == 1)
        {
            if (*x < n - 1)
                *x = *x + 1;
            else
                *y = *y + 1;
            *dir = 2;
        }
        else if (*x == 0 && *dir == 2)
        {
            if (*y < n - 1)
                *y = *y + 1;
            else
                *x = *x + 1;
            *dir = 1;
        }
        else if (*x == n - 1 && *dir == 1)
        {
            *y = *y + 1;
            *dir = 2;
        }
        else if (*y == n - 1 && *dir == 2)
        {
            *x = *x + 1;
            *dir = 1;
        }
        else if (*dir == 1)
        {
            *x = *x + 1;
            *y = *y - 1;
        }
        else if (*dir == 2)
        {
            *x = *x - 1;
            *y = *y + 1;
        }
        return true;
    }
    
    int main()
    {
        int x = 0; 
        int y = 0;
        int dir = 0;
        int n;
        scanf("%d", &n);
        int * arr = (int *)malloc(sizeof(int) * n * n);
        arr[0] = 0;
        for (int i = 1; i < n * n; i++)
        {
            movenext(&x, &y, &dir, n);
            arr[x * n + y] = i;
        }
        for (int i = 0; i < n * n; i++)
        {
            printf("%d\t", arr[i]);
            if (i % n == n - 1) printf("\n");
        }
        return 0;
    }
    
    
    

    图片说明

    如果问题得到解决,请点我回答左上角的采纳和向上的箭头,谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line