《算法竞赛入门经典》第三章(蛇形填数)
来源:互联网 发布:大数据公司被收购 编辑:程序博客网 时间:2024/06/05 20:27
自己的
#pragma warning(disable:4996)#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<algorithm>#include<iostream>#include<time.h>#include<map> #include<set>#include<sstream>#include<functional>#include<cassert>#include<list>#include<iterator>#include<utility>#include <stdexcept> #include <sstream>#include <fstream> #include<unordered_map>#include<unordered_set>using namespace std;using namespace std::placeholders;int a[1050][1050];int main(){ memset(a, 0, sizeof(a)); int n; while (cin >> n) { memset(a, 0, sizeof(a)); int count = 1; int x, y; x = 0; y = n - 1; while (count <= n*n) { int i = 0; while (i < n&&a[x][y] == 0) { a[x++][y]=count++; i++; } x--; y--; i = 0; while (i < n-1&&a[x][y] == 0) { a[x][y--] = count++; i++; } y++; x--; i = 0; while (i < n-1&&a[x][y] == 0) { a[x--][y] = count++; i++; } x++; y++; i = 0; while (i < n-2&&a[x][y] == 0) { a[x][y++] = count++; i++; } y--; x++; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) printf("%5d", a[i][j]); cout << endl; } } return 0;}
白书的
#pragma warning(disable:4996)#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<algorithm>#include<iostream>#include<time.h>#include<map> #include<set>#include<sstream>#include<functional>#include<cassert>#include<list>#include<iterator>#include<utility>#include <stdexcept> #include <sstream>#include <fstream> #include<unordered_map>#include<unordered_set>using namespace std;using namespace std::placeholders;int a[1050][1050];int main(){ int n, x, y, tot = 0; scanf("%d", &n); memset(a, 0, sizeof(a)); tot = a[x = 0][y = n - 1] = 1; while (tot < n*n) { while (x + 1 < n&&!a[x + 1][y])a[++x][y] = ++tot; while (y - 1 >= 0 && !a[x][y - 1])a[x][--y] = ++tot; while (x - 1 >= 0 && !a[x - 1][y])a[--x][y] = ++tot; while (y + 1 <= n&&!a[x][y+1])a[x][++y] = ++tot; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) printf("%5d", a[i][j]); cout << endl; } return 0;}
= =刘如佳这程序我感觉真心不好理解
0 0
- 《算法竞赛入门经典》第三章(蛇形填数)
- 算法竞赛入门经典 蛇形填数
- {算法竞赛入门经典}第三章 蛇形填数 边界预判
- 算法竞赛入门经典: 第三章 数组和字符串 3.3蛇形填数
- 算法竞赛入门经典 例3-2蛇形填数
- 算法竞赛入门经典 例题 3-2 蛇形填数
- 算法竞赛入门经典ch3_ex2蛇形填数
- 刘汝佳p39,3-3(蛇形填数)算法竞赛入门经典第二版
- 算法之路二:刘汝佳算法竞赛入门经典 3.3蛇形填数
- 算法竞赛入门 例题3-2 蛇形填数P35
- 【经典算法】:蛇形填数
- 算法竞赛_蛇形填数_C++
- 算法竞赛入门经典(第三章)
- 算法竞赛入门经典 蛇形数组
- 算法:蛇形填数
- 【算法】蛇形填数
- 算法竞赛入门经典 第三章
- 算法竞赛入门经典第三章笔记
- 一个菜鸟java码农的成长之路(4):用LinkedList模拟栈结构
- 关于AXIS1.4实现webservice接口部署的基础实例
- 【NOIP2012模拟10.6】购买
- KMP
- 索引总结
- 《算法竞赛入门经典》第三章(蛇形填数)
- 从宏观到微观理解coding
- POJ 1984 Navigation Nightmare
- (Java)LeetCode-56. Merge Intervals
- LeetCode144—Binary Tree Preorder Traversal
- OpenCV 3.0 with Java Chapter 2 Handling Matrices
- 数据结构之线性结构 --循环单链表
- nodemailer模块
- 洛谷 P1396 营救