09-语言入门-09-蛇形填数

题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=33

描述
在n*n方陈里填入1,2,...,n*n,要求填成蛇形。例如n=4时方陈为:
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4

输入
直接输入方陈的维数,即n的值。(n<=100)

输出
输出结果是蛇形方阵。

样例输入
3

样例输出
7 8 1
6 9 2
5 4 3

 

分析:

1.写代码解决问题首先要确定哪些是不变的。

2.然后是哪些是变的。

在蛇形填充数据的时候,一圈(从右侧、下侧、左侧、上侧)下来,要么行不变,列变,要么列不变,行变。

另外存储结构的选择,既可以选择二维数组存储,也可以通过一维数组进行存储,此题的关键应该是下标索引的处理。

 

代码:

 

#include <stdio.h>
#include <stdlib.h>
static void setArr(int **arr,int readNum);
static void printArr(int **arr,int num);
int main()
{
     int readNum = 0;
     scanf("%d",&readNum);
     getchar();
     int **arr = (int**)malloc(sizeof(int*)*readNum);
     int rowIndex = 0;
     for(;rowIndex < readNum;++rowIndex)
     {
          arr[rowIndex] = (int*)malloc(sizeof(int)*readNum);
     }
     setArr(arr,readNum);
     printArr(arr,readNum);
     rowIndex = 0;
     for(;rowIndex < readNum;++rowIndex)
     {
          free(arr[rowIndex]);
     }
     free(arr);
     return 0;
}
static void setArr(int **arr,int readNum)
{
     int i=0;
     int flag = readNum;
     int colIndex = readNum-1;
     int rowIndex = 0;
     int maxNum = readNum*readNum;
     while(i<maxNum) //如果是偶数,则最大一个值在上侧执行完毕后就结束了
     {
          colIndex = flag-1;
          if(i == maxNum-1) //如果是奇数方阵,则最后一个数是最后一个最大值,特殊处理后结束
          {
               arr[rowIndex][colIndex] = ++i;
               break;
          }
          //右侧,行+,列不变
          for(;rowIndex<flag-1 && i<=maxNum;++rowIndex)
          {
               arr[rowIndex][colIndex] = ++i;
          }
          //下侧,行不变 列-
          for(;colIndex>readNum-flag && i<=maxNum;--colIndex)
          {
               arr[rowIndex][colIndex] = ++i;
          }
          //左侧,行-,列不变
          for(;rowIndex>readNum-flag && i<=maxNum;--rowIndex)
          {
               arr[rowIndex][colIndex] = ++i;
          }
          //上侧,行不变,列++
          for(;colIndex<flag-1 && i<=maxNum;++colIndex)
          {
               arr[rowIndex][colIndex] = ++i;
          }
          --flag;
          ++rowIndex;
     }
}
static void printArr(int **arr,int num)
{
     int     rowIndex = 0;
     for(;rowIndex<num;++rowIndex)
     {
          int colIndex = 0;
          for(;colIndex < num;++colIndex)
          {
               printf("%d ",arr[rowIndex][colIndex]);
          }
          printf("\n");
     }
}

 

也可以通过一维数组进行存储的方式:

//创建二维数组
voidSnakePrint::create_array(int n)
{
if (n<=0)
    {
return;
    }
releaseData();
m_inputN = n;
m_array.resize(n*n);

}

2.对方阵赋值

//构建蛇形数组
voidSnakePrint::cal_array()
{
int totalNumber = m_inputN * m_inputN;
int currentIndex = 0;
int rowIndex = 0;
int colIndex = m_inputN-1;
m_array.at(rowIndex*m_inputN+colIndex) = ++currentIndex;
while (currentIndex < totalNumber)
    {
//右侧
while ( (rowIndex + 1) < m_inputN && m_array.at((rowIndex + 1)*m_inputN+colIndex) == 0)
        {
m_array.at((++rowIndex)*m_inputN+colIndex) = ++currentIndex;
        }
//下侧
while ( (colIndex - 1) >= 0 && m_array.at(rowIndex*m_inputN+colIndex - 1) == 0)
        {
m_array.at(rowIndex*m_inputN+(--colIndex)) = ++currentIndex;
        }
//左侧
while ( (rowIndex - 1) >= 0 && m_array.at((rowIndex - 1)*m_inputN+colIndex) == 0)
        {
m_array.at((--rowIndex)*m_inputN+colIndex) = ++currentIndex;
        }
//上侧
while ( (colIndex + 1) < m_inputN && m_array.at(rowIndex*m_inputN+colIndex + 1) == 0)
        {
m_array.at(rowIndex*m_inputN+(++colIndex)) = ++currentIndex;
        }
    }
}

打印结果

//打印结果
voidSnakePrint::print()
{
cout<<"----------array content ---------"<<endl;
for (int rowIndex = 0; rowIndex < m_inputN; ++rowIndex)
    {
for (int colIndex = 0; colIndex < m_inputN; ++colIndex)
        {
cout<<setw(3)<<m_array.at(rowIndex*m_inputN+colIndex)<<" ";
        }
cout<<endl;
    }
cout<<"----------array content ---------"<<endl;
}

结果

Image(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值