题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=7
先把外围填好再考虑深入循环。递归没成功,就用了循环。这段代码虽然解决了问题,但是并不好。
#include <stdio.h>
void Full(int a[][101], int n,int count)
{
int i,j,t,temp; //temp记录深入程度
t = n*n; //记录最大数
for(i = 1, j = n, temp = 1; count <= t; i++,temp++)
{
while(i <= j) //右边一列
{
if(count == t) //奇数情况
{
a[i][j] = count;
return;
}
a[i][j] = count;
i++;
count++;
}
i--;count--;
while(j >= temp) //下边一列
{
a[i][j] = count;
j--;
count++;
}
j++;count--;
while(i >= temp) //左边一列
{
a[i][j] = count;
i--;
count++;
}
i++;count--;
while(j <= n-temp) //上边一列
{
a[i][j] = count;
j++;
count++;
}
j--;
}
}
int main()
{
int a[101][101] = {0};
int i,j,n;
scanf("%d",&n);
Full(a,n,1);
for(i = 1; i <= n; i++ )
{
for(j = 1; j <= n; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
分享到:
相关推荐
蛇形填数的c语言
述 在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) 输出 输出结果是蛇形方陈。
#include #include #include #include #include using namespace std; typedef long long ll; const int maxn=1100; int a[maxn][maxn]; int main(){ int n,x; scanf("%d",&n); while(n--){ ...}
只是简单实现蛇形填数C++,还没考虑提高时间效率空间效率的提高。
while 蛇形填数.c
和教材上不一样的写法,自己写的蛇形方阵的代码。谢谢!
这个文件可以提升你的算法,答题的效率
问题描述: 当给出 N*N 的矩阵,要求用程序填入下列形式的数: [1] 倒填 [2] 蛇形填数 [3] 回转填数
蛇形矩阵leetcode dingjianhub 的 leetcode 题解 写在前面 这里是 的 leetcode 刷题的笔记。...包含了已经提交的题目的代码、...蛇形填数 Medium 前缀积;后缀积 Easy 分情况讨论;多个数字交换 题目名(点击跳转题目) 笔记
蛇形矩阵leetcode 《剑指 ...平台题解 写在前面 这里是 在 ...刷 《剑指 Offer》算法题的笔记。...包含了已经提交的题目的...蛇形填数 题目名(点击跳转题目) 笔记链接(点击跳转链接) 难度评级 关键词 Easy 栈 Easy Easy 快慢指针
高次快速求幂,卡片游戏,Cantor的数表,6174代码实现,蛇形填数等问题的代码实现
在一个N*N的方阵中,填入1,2,……N 共N个数,并要求构成如下的格式:N, 例: N=4 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 输入 多组数据,每行读入一个N. 输出 对应输出N*N的蛇形矩阵,每个数字...