蛇形填数
2016-04-16 14:07
204 查看
#include<stdio.h>
#include<string.h>
int main()
{
int a[10][10], n,tot= 0, x, y;
memset(a, 0, sizeof(a));//刚开始将数组元素全部清零
tot = a[x=0][y=0] = 1;
scanf("%d", &n);
while(tot < n*n)//按蛇形右下左上 不能填已经走过的格子 和不能越界
{
while(y < n-1 && !a[x][y+1])
a[x][++y] = ++tot;
while(x < n-1 && !a[1+x][y])
a[++x][y] = ++tot;
while(x >= 1 && !a[x-1][y])
a[--x][y] = ++tot;
while(y >= 1 && !a[x][y-1])
a[x][--y] = ++tot;
}
for(x = 0; x < n; x++)
{
for(y = 0; y < n; y++)
printf("%3d",a[x][y]);
printf("\n");
}
}
#include<string.h>
int main()
{
int a[10][10], n,tot= 0, x, y;
memset(a, 0, sizeof(a));//刚开始将数组元素全部清零
tot = a[x=0][y=0] = 1;
scanf("%d", &n);
while(tot < n*n)//按蛇形右下左上 不能填已经走过的格子 和不能越界
{
while(y < n-1 && !a[x][y+1])
a[x][++y] = ++tot;
while(x < n-1 && !a[1+x][y])
a[++x][y] = ++tot;
while(x >= 1 && !a[x-1][y])
a[--x][y] = ++tot;
while(y >= 1 && !a[x][y-1])
a[x][--y] = ++tot;
}
for(x = 0; x < n; x++)
{
for(y = 0; y < n; y++)
printf("%3d",a[x][y]);
printf("\n");
}
}
相关文章推荐
- using ViewPager making a flush
- 网易游戏 产品项目管理面经(内推)
- 2016手游设计分辨率推荐
- Android 四大组件之一------Activity
- 操作系统hosts文件
- 282. Expression Add Operators
- Spring AOP 学习例子
- marathon参考(11):ports端口设置
- PayPal高级工程总监:读完这100篇论文 就能成大数据高手(附论文下载)
- 源码分析Flume启动过程
- B+/-Tree原理及mysql的索引分析
- 【Arduino官方教程第一辑】示例程序 1-1 读取模拟信号、串口操作
- Java Web 基础环境搭建
- 激光加速光帆可行么?
- bzoj4241 历史研究
- ti processor sdk linux am335x evm /bin/setup-host-check.sh hacking
- 改变软键盘Enter键功能
- zabbix流量汇总脚本
- 2014阿里实习生面试题——MySQL如何实现索引的
- Java集合(五):Set集