baybaye 2015-11-06 14:16 采纳率: 70%
浏览 1538
已采纳

c语言简单编程

Input array size N (1~20)
Using Two-Dimensional array
输入数组大小氮(1 - 20)
二维数组![图片说明](https://img-ask.csdn.net/upload/201511/06/1446819387_646558.png)图片说明

  • 写回答

7条回答 默认 最新

  • Meditator_hkx 2015-11-06 17:43
    关注

    楼上站着说话不腰疼。
    这道题只给思路,但是你那个最关键的思路就没给。
    我以为自己十分钟可以搞定这题,结果弄了一个小时。。。
    给出经过测试的C语言源码如下:

     #include "stdio.h"
    #include "stdlib.h"
    #define NUM 5
    
    void right_add(int a[NUM][NUM],int line,int row,int count); //Note that it can't be written as a[][] here(with an error called "Without subscript")
    void down_add(int a[NUM][NUM],int line,int row,int count);
    void left_add(int a[NUM][NUM],int line,int row,int count);
    void up_add(int a[NUM][NUM],int line,int row,int count);
    void print(int a[NUM][NUM],int n);
    
    
    int main(int argc, char* argv[])
    {
        int i,j;
        int result[NUM][NUM];
        for(i = 0;i < NUM;i++)
            for(j = 0;j < NUM;j++)
                result[i][j] = 0;
    
        int num = NUM * NUM;
        right_add(result,0,0,1);   //The first move is always towards right
    
        return 0;
    }
    
    void right_add(int a[NUM][NUM],int line,int row,int count) {  //Move to right position until there are limits
        while(a[line][row] == 0) {
            a[line][row] = count;
            count++;
            if (count > NUM*NUM) {
                print(a,NUM);
                exit(0);
            }
            row++;
    
            if(row % NUM == 0 || a[line][row] != 0) { //When it reaches the border or its right is a valid number,goto down_add
                row--;
                line++;
                down_add(a,line,row,count);
            }
        }
    }
    
    void down_add(int a[NUM][NUM],int line,int row,int count) { //Move to downside position until there are limits
        while(a[line][row] == 0) {
            a[line][row] = count;
            count++;
            if (count > NUM*NUM) {
                print(a,NUM);
                exit(0);
            }
            line++;
    
            if(line % NUM == 0 || a[line][row] != 0) { //When it reaches the border or its downside is a valid number,goto left_add
                line--;
                row--;
                left_add(a,line,row,count);
            }
        }
    }
    
    void left_add(int a[NUM][NUM],int line,int row,int count) { //Move to left position until there are limits
        while(a[line][row] == 0) {
            a[line][row] = count;
            count++;
            if (count > NUM*NUM) {
                print(a,NUM);
                exit(0);
            }
            row--;
    
            if(row == -1 || a[line][row] != 0) { //When it reaches the border or its left is a valid number,goto up_add
                row++;
                line--;
                up_add(a,line,row,count);
            }
        }
    }
    
    void up_add(int a[NUM][NUM],int line,int row,int count) { //Move to upside position until there are limits
        while(a[line][row] == 0) {
            a[line][row] = count;
            count++;
            if (count > NUM*NUM) {
                print(a,NUM);
                exit(0);
            }
            line--;
    
            if(line == -1 || a[line][row] != 0) { //When it reaches the border or its left is a valid number,goto up_add
                line++;
                row++;
                right_add(a,line,row,count);
            }
        }
    }
    
    void print(int a[NUM][NUM],int n) { //Print out the result we get
        int i,j;
        for(i = 0;i < n;i++) {
            for(j = 0;j < n;j++) {
                printf("%3d",a[i][j]);   //Formatting the output
                if ((j+1) % n == 0)      //goto next line
                    printf("\n");
            }
        }
    }
    

    测试结果:
    图片说明
    图片说明
    图片说明
    再放个大招:
    图片说明

    附:当规模大了以后,print函数的格式化输出需要调整一下。
    题主看看程序思路,如果哪里不理解欢迎继续交流,我打算把它写到我的博客里了~
    当然,这个代码是可以优化的,我到时再用指针重写一遍~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)