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;
    }
    
    
    

    图片说明

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

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

报告相同问题?

悬赏问题

  • ¥20 关于php中URL传递GET全局变量的问题
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件