标题:蚊香的代码咋弄啊
只看楼主
wjats
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-11-15
 问题点数:0 回复次数:5 
蚊香的代码咋弄啊
输入两个整数,第一个是行数m(不小于2,不大于23),第二个是列数(不小于2,不大于19)
按蚊香的形式旋转输出从1到m×n的整数。
如:输入3 4
     输出
     1 10 9 8
     2 11 12 7
     3 4 5 6
搜索更多相关主题的帖子: 代码 输入 小于 大于 输出 
2018-11-15 17:37
no1xijin
Rank: 9Rank: 9Rank: 9
来 自:江西
等 级:贵宾
威 望:20
帖 子:211
专家分:1207
注 册:2015-7-8
得分:0 
百度

126邮箱联系方式:no1xijin@126. com
2018-11-15 19:01
桃花开十里香
Rank: 2
等 级:论坛游民
威 望:1
帖 子:15
专家分:24
注 册:2018-11-8
得分:0 
这样问估计凉凉,我知道的。
如果是以前的我或者会敲敲,但现在不同咯~
各人有各事,最后建议翻翻类似的帖子。
搜搜蛇型矩阵。

告辞
2018-11-15 20:02
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:0 
定义四个方向
1 0   下
0 1   右
-1 0  上
0 -1  左

当前坐标 x y
下移动
x+=1 y+=0
右移动
x+=0 y+=1
注入此类

剩下的自己解决

https://zh.
2018-11-15 20:14
Tomorrw_I
Rank: 10Rank: 10Rank: 10
等 级:禁止访问
威 望:6
帖 子:406
专家分:1712
注 册:2018-10-16
得分:0 
// SpiralMatrix_2016_1.cpp : 定义控制台应用程序的入口点。
//2016年1月份螺旋矩阵! X-Bag编写

#include "stdafx.h"
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    while (1)
    {
        int** array;
        int m, n;
        int step = 1;
        int point = 1;
        enum enumType { DOWN, RIGHT, UP, LEFT }MOVE = DOWN;     //定义行走方向! 初始方向向下走!
        cout << "请输入行数:";
        cin >> m;
        cout << endl;
        cout << "请输入列数:";
        cin >> n;
        cout << endl;
        //动态分配m行n列的二维数组空间---------------------------
        array = (int**)malloc(sizeof(int*)*m);
        for (int i = 0; i < m; i++)
        {
            array[i] = (int*)malloc(sizeof(int)*n);
        }

        //动态数组赋值从1到m*n-----------------------------------
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                array[i][j] = point++;
            }

        }
        //动态数组数据打印测试-----------------------------------
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << array[i][j] << "\t";
            }
            cout << endl;
        }


        //螺旋矩阵按步走------------------------------------------
        int   x = n, y = m;//这个是步长控制,在x方向可走n步,y方向可走m步
        int i = 0, j = 0;
        while (step <= m*n)//循环结束条件
        {
            switch (MOVE)
            {
            case DOWN:   //向下走
            {
                             for (int k = 0; k < y; k++)
                             {
                                 array[i][j] = step++;
                                 i++;
                             }
                             i--, j++;
                             MOVE = RIGHT;
                             x--;
            }
                break;
            case RIGHT:
            {
                          for (int k = 0; k < x; k++)
                          {
                              array[i][j] = step++;
                              j++;
                          }
                          j--, i--;
                          MOVE = UP;
                          y--;
            }
                break;
            case UP:
            {
                       for (int k = 0; k < y; k++)
                       {
                           array[i][j] = step++;
                           i--;
                       }
                       i++, j--;
                       MOVE = LEFT;
                       x--;
            }
                break;
            case LEFT:
            {
                         for (int k = 0; k < x; k++)
                         {
                             array[i][j] = step++;
                             j--;
                         }
                         j++, i++;
                         MOVE = DOWN;
                         y--;
            }
                break;
            default:
                break;
            }
        }

        //螺旋矩阵排序打印------------------------------------------
        cout <<m<<"行"<<n<<"列的螺旋矩阵,展示如下:\n";
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << array[i][j] << "\t";
            }
            cout << endl;
        }


        //释放动态开辟的二位数组空间
        for (int i = 0; i < m; i++)
        {
            free(array[i]);
        }
        free(array);
    }
    system("pause");
    return 0;
}

交作业去吧!

学C有用吗?
学C++有前途吗?
数据库有必要学吗?
……
别问,你还什么都不会,学就对了
……
2018-11-15 20:30
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:0 
以下是引用Tomorrw_I在2018-11-15 20:30:21的发言:

// SpiralMatrix_2016_1.cpp : 定义控制台应用程序的入口点。
//2016年1月份螺旋矩阵! X-Bag编写

#include "stdafx.h"
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    while (1)
    {
        int** array;
        int m, n;
        int step = 1;
        int point = 1;
        enum enumType { DOWN, RIGHT, UP, LEFT }MOVE = DOWN;     //定义行走方向! 初始方向向下走!
        cout << "请输入行数:";
        cin >> m;
        cout << endl;
        cout << "请输入列数:";
        cin >> n;
        cout << endl;
        //动态分配m行n列的二维数组空间---------------------------
        array = (int**)malloc(sizeof(int*)*m);
        for (int i = 0; i < m; i++)
        {
            array = (int*)malloc(sizeof(int)*n);
        }

        //动态数组赋值从1到m*n-----------------------------------
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                array[j] = point++;
            }

        }
        //动态数组数据打印测试-----------------------------------
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << array[j] << "\t";
            }
            cout << endl;
        }


        //螺旋矩阵按步走------------------------------------------
        int   x = n, y = m;//这个是步长控制,在x方向可走n步,y方向可走m步
        int i = 0, j = 0;
        while (step <= m*n)//循环结束条件
        {
            switch (MOVE)
            {
            case DOWN:   //向下走
            {
                             for (int k = 0; k < y; k++)
                             {
                                 array[j] = step++;
                                 i++;
                             }
                             i--, j++;
                             MOVE = RIGHT;
                             x--;
            }
                break;
            case RIGHT:
            {
                          for (int k = 0; k < x; k++)
                          {
                              array[j] = step++;
                              j++;
                          }
                          j--, i--;
                          MOVE = UP;
                          y--;
            }
                break;
            case UP:
            {
                       for (int k = 0; k < y; k++)
                       {
                           array[j] = step++;
                           i--;
                       }
                       i++, j--;
                       MOVE = LEFT;
                       x--;
            }
                break;
            case LEFT:
            {
                         for (int k = 0; k < x; k++)
                         {
                             array[j] = step++;
                             j--;
                         }
                         j++, i++;
                         MOVE = DOWN;
                         y--;
            }
                break;
            default:
                break;
            }
        }

        //螺旋矩阵排序打印------------------------------------------
        cout <<m<<"行"<<n<<"列的螺旋矩阵,展示如下:\n";
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << array[j] << "\t";
            }
            cout << endl;
        }


        //释放动态开辟的二位数组空间
        for (int i = 0; i < m; i++)
        {
            free(array);
        }
        free(array);
    }
    system("pause");
    return 0;
}

交作业去吧!

太稚嫩
动态分配多余

移动实现太生硬
控制变量实在太多了

https://zh.
2018-11-15 21:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-491301-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.250182 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved